我是Android应用程序开发的初学者,正在制作一个简单的应用程序来添加和减去。我在模拟器或手机(Galaxy Y duos)上传时会反复出现此错误。
logcat的:
06-05 16:22:17.402: E/Trace(1759): error opening trace file: No such file or directory (2)
06-05 16:22:17.513: E/AndroidRuntime(1759): FATAL EXCEPTION: main
06-05 16:22:17.513: E/AndroidRuntime(1759): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.levelone/com.example.levelone.MainActivity}: java.lang.NullPointerException
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.os.Looper.loop(Looper.java:137)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-05 16:22:17.513: E/AndroidRuntime(1759): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 16:22:17.513: E/AndroidRuntime(1759): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 16:22:17.513: E/AndroidRuntime(1759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-05 16:22:17.513: E/AndroidRuntime(1759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-05 16:22:17.513: E/AndroidRuntime(1759): at dalvik.system.NativeStart.main(Native Method)
06-05 16:22:17.513: E/AndroidRuntime(1759): Caused by: java.lang.NullPointerException
06-05 16:22:17.513: E/AndroidRuntime(1759): at com.example.levelone.MainActivity.onCreate(MainActivity.java:22)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.Activity.performCreate(Activity.java:5104)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-05 16:22:17.513: E/AndroidRuntime(1759): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-05 16:22:17.513: E/AndroidRuntime(1759): ... 11 more
Mainactivity.java:
package com.example.levelone;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add = (Button) findViewById(R.id.badd);
sub = (Button) findViewById(R.id.bsub);
display = (TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
}
}
fragment_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.example.levelone.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/tvdisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Your score is 0" />
<Button
android:id="@+id/badd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tvdisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="Add one" />
<Button
android:id="@+id/bsub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/badd"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Subtract one" />
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.levelone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"
android:maxSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.levelone.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>
请帮忙。我无法继续进一步
答案 0 :(得分:0)
替换MainActivity java文件中的以下内容
setContentView(R.layout.activity_main);
与
setContentView(R.layout.fragment_main);