2014-10-08 2 views
-2

Может ли кто-нибудь сказать мне, почему мое приложение неожиданно закрывается?NulPointerException, приложение Android неожиданно закрывается

Ниже приведена ошибка LogCat, но я понятия не имею, что они означают, нужна помощь.

ошибка LogCat

10-08 11:15:00.556: E/AndroidRuntime(275): FATAL EXCEPTION: main 
10-08 11:15:00.556: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.atms/com.fyp.atms.MainActivity}: java.lang.NullPointerException 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-08 11:15:00.556: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method) 
10-08 11:15:00.556: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521) 
10-08 11:15:00.556: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-08 11:15:00.556: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-08 11:15:00.556: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method) 
10-08 11:15:00.556: E/AndroidRuntime(275): Caused by: java.lang.NullPointerException 
10-08 11:15:00.556: E/AndroidRuntime(275): at com.fyp.atms.MainActivity.onCreate(MainActivity.java:30) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-08 11:15:00.556: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
10-08 11:15:00.556: E/AndroidRuntime(275): ... 11 more 

MainActivity.java

package com.fyp.atms; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.memetix.mst.language.Language; 
import com.memetix.mst.translate.Translate; 

public class MainActivity extends ActionBarActivity implements OnClickListener{ 

    TextView tled; 
    EditText toTl; 
    Button translate; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Translate.setClientId("bharatnakka"); 
     Translate.setClientSecret("+HLa1sMAlW6Kw6XpNcIo+m2DxLZySLpmV2BgT96sA2s="); 

     translate.setOnClickListener(this); 

     translate = (Button) findViewById(R.id.btnT); // translate button 
     tled = (TextView) findViewById(R.id.Ted); // translated 
     toTl = (EditText) findViewById(R.id.toT); // to be translate 

     translate.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       switch(v.getId()){ 
       case R.id.btnT: 
        String input = toTl.getText().toString(); 
        try { 
         String output = Translate.execute(input, Language.AUTO_DETECT, Language.FRENCH); 

        toTl.setText(input); 
        tled.setText(output); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 


      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 
} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.fyp.atms.MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="28dp" 
     android:text="@string/atms" 
     android:gravity="center" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/toT" 
     android:hint="Type to translate..." 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="40dp" 
     android:ems="10" /> 

    <Button 
     android:id="@+id/btnT" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Translate" /> 

    <TextView 
     android:id="@+id/Ted" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/toT" 
     android:layout_below="@+id/btnT" 
     android:layout_marginTop="57dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

Банкоматы Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.fyp.atms" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Firstpage" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.fyp.atms.MAINACTIVITY" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

удалить эту строку ** translate.setOnClickListener (это); ** –

+0

ничего не предложено из, но теперь это не перевод слова. Я уже установил TextView, но переведенный вывод не появился. – Danzeeeee

ответ

2

Изменить этот порядок из

translate.setOnClickListener(this); 
translate = (Button) findViewById(R.id.btnT); 

в

translate = (Button) findViewById(R.id.btnT); 
translate.setOnClickListener(this); 

Вы должны инициализировать translate Button первый, а затем установите OnClickListner() в Button

+0

Спасибо большое! Он работает: D – Danzeeeee

0

translate Кнопка никогда не была инициализирована ,

Фактически, это инициализировано, но слишком поздно, сначала сделайте translate = (Button) findViewById(R.id.btnT);, прежде чем что-либо делать с этим объектом.

+0

Это решение работает, спасибо – Danzeeeee

-1

Измените свой класс, как указано ниже!


MainActivity.java

package com.fyp.atms; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.memetix.mst.language.Language; 
import com.memetix.mst.translate.Translate; 

public class MainActivity extends ActionBarActivity implements OnClickListener{ 

    TextView tled; 
    EditText toTl; 
    Button translate; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Translate.setClientId("bharatnakka"); 
     Translate.setClientSecret("+HLa1sMAlW6Kw6XpNcIo+m2DxLZySLpmV2BgT96sA2s="); 

     translate = (Button) findViewById(R.id.btnT); // translate button 
     tled = (TextView) findViewById(R.id.Ted); // translated 
     toTl = (EditText) findViewById(R.id.toT); // to be translate 

     translate.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       switch(v.getId()){ 
       case R.id.btnT: 
        String input = toTl.getText().toString(); 
        try { 
         String output = Translate.execute(input, Language.AUTO_DETECT, Language.FRENCH); 

        toTl.setText(input); 
        tled.setText(output); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 


      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

Спасибо за помощь – Danzeeeee

Смежные вопросы