有谁能告诉我为什么我的申请意外关闭了?
以下是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>
atms 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 :(得分:2)
从
更改此订单 translate.setOnClickListener(this);
translate = (Button) findViewById(R.id.btnT);
到
translate = (Button) findViewById(R.id.btnT);
translate.setOnClickListener(this);
您应首先初始化translate Button
,然后将OnClickListner()
设置为Button
答案 1 :(得分:0)
translate
按钮从未初始化。
事实上,它已初始化但为时已晚,在对此对象执行任何其他操作之前先执行translate = (Button) findViewById(R.id.btnT);
。
答案 2 :(得分:-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
}
}