我正在使用我的andorid应用程序遇到一些问题我目前正在eclipse包中开发。 我想在点击按钮上进行简单的文本更改,但遗憾的是,当我在设备上运行应用程序时,它会停止工作。应用程序在我的设备上崩溃,但在eclipse中没有错误。
这是我的代码:
final TextView chores = (TextView) findViewById(R.id.chores);
Button choreButton = (Button) findViewById(R.id.choreButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String next = "Go make some Coffee.";
chores.setText(next);
}
};
choreButton.setOnClickListener(listener);
感谢您的帮助! :)
所以这是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.zargi.choreme.MainActivity$PlaceholderFragment"
android:background="#6EFF70" >
<TextView
android:id="@+id/choremehead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you bored? Then go and..."
android:textSize="22sp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/chores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/choremehead"
android:layout_centerVertical="true"
android:text="@string/chore"
android:textColor="@color/textColor"
android:textSize="22sp" />
<Button
android:id="@+id/choreButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/chores"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:text="Enlighten me!" />
</RelativeLayout>
这就是我在java中写的
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView chores = (TextView) findViewById(R.id.chores);
Button choreButton = (Button) findViewById(R.id.choreButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String next = "Go make some Coffee.";
chores.setText(next);
}
};
choreButton.setOnClickListener(listener);
}
}
这里是logcat,现在有一些错误在之前没有...
09-02 04:48:06.650: D/AndroidRuntime(972): Shutting down VM
09-02 04:48:06.650: W/dalvikvm(972): threadid=1: thread exiting with uncaught exception (group=0xb2a9dba8)
09-02 04:48:06.670: E/AndroidRuntime(972): FATAL EXCEPTION: main
09-02 04:48:06.670: E/AndroidRuntime(972): Process: com.zargi.choreme, PID: 972
09-02 04:48:06.670: E/AndroidRuntime(972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zargi.choreme/com.zargi.choreme.MainActivity}: java.lang.NullPointerException
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.os.Handler.dispatchMessage(Handler.java:102)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.os.Looper.loop(Looper.java:136)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 04:48:06.670: E/AndroidRuntime(972): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 04:48:06.670: E/AndroidRuntime(972): at java.lang.reflect.Method.invoke(Method.java:515)
09-02 04:48:06.670: E/AndroidRuntime(972): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 04:48:06.670: E/AndroidRuntime(972): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 04:48:06.670: E/AndroidRuntime(972): at dalvik.system.NativeStart.main(Native Method)
09-02 04:48:06.670: E/AndroidRuntime(972): Caused by: java.lang.NullPointerException
09-02 04:48:06.670: E/AndroidRuntime(972): at com.zargi.choreme.MainActivity.onCreate(MainActivity.java:38)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.Activity.performCreate(Activity.java:5231)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-02 04:48:06.670: E/AndroidRuntime(972): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-02 04:48:06.670: E/AndroidRuntime(972): ... 11 more
09-02 04:48:48.726: I/Process(972): Sending signal. PID: 972 SIG: 9
androidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zargi.choreme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.zargi.choreme.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
据说我没有看到logcat中的任何错误,但我的应用程序在移动电话上崩溃,就像在AVD上一样。我也是java编程的新手,很抱歉,如果我犯了任何愚蠢的错误:D
答案 0 :(得分:0)
这里有几个选项
chores
是null
choreButton
是null
在Android中插入日志语句,最好是通过框架或Log.d(String, String)
(甚至是好的System.out.println(String)
作品)。
像
Log.d("nullcheck", "chores?" + (chores == null) + " chorebutton?" + (choreButton == null));
但是,您可能会尝试在setContentView()
中初始化之前的视图<!> onCreate()
。就像下面这样,你很好(如果,并且只有当你的xml正确充气,并且id存在于xml上)
public class MyActivity extends Activity {
TextView chores;
Button choreButton;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
chores = (TextView)findViewById(R.id.chores);
choreButton = (Button)findViewById(R.id.choreButton);
//Here a log statement. Insert it in your code, run the app, and examine the output
Log.d("nullcheck", "chores?" + (chores == null) + " chorebutton?" + (choreButton == null));
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String next = "Go make some Coffee.";
chores.setText(next);
}
};
choreButton.setOnClickListener(listener);
}
}
话虽如此,必须在xml中声明了R.id.chores
和R.id.choreButton
(在我的示例activity_main.xml
中)
<LinearLayout
... >
<TextView
android:id="@+id/chores"
... />
<Button
android:id="@+id/choreButton"
... />
</LinearLayout>
答案 1 :(得分:0)
确定。因此,必须检查Button
和TextView
是否在activity_main.xml
文件中。