Java 运行应用程序时主活动出错
我一直在线关注Android开发教程,但当我在模拟器上运行应用程序时,由于出现错误,它会关闭 我用谷歌搜索了第一个错误,但没有用。但是通过查看日志猫的其余部分,我注意到它抛出了来自主要活动的错误。我认为这可能是我的清单文件有问题,但我不确定具体是什么问题 我的包名是com.example.androidtutorials 有没有人能告诉我在接下来的教程中我做了哪些错误的事情。我的清单文件如下:Java 运行应用程序时主活动出错,java,android,android-activity,android-logcat,Java,Android,Android Activity,Android Logcat,我一直在线关注Android开发教程,但当我在模拟器上运行应用程序时,由于出现错误,它会关闭 我用谷歌搜索了第一个错误,但没有用。但是通过查看日志猫的其余部分,我注意到它抛出了来自主要活动的错误。我认为这可能是我的清单文件有问题,但我不确定具体是什么问题 我的包名是com.example.androidtutorials 有没有人能告诉我在接下来的教程中我做了哪些错误的事情。我的清单文件如下: <?xml version="1.0" encoding="utf-8"?> <ma
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtutorials"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidtutorials.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>
<activity
android:name=".Numbers"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.NUMBERS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".InternalStorage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.INTERNALSTORAGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Reading"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.READING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SaveToSD"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SAVETOSD" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
来自主类的onCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
width = (Button)findViewById(R.id.button1);
height = (Button)findViewById(R.id.button2);
calc = (Button)findViewById(R.id.button3);
area = (TextView)findViewById(R.id.textView1);
width.setOnClickListener(this);
height.setOnClickListener(this);
calc.setOnClickListener(this);
}
因为这行给你的是
NPE
width.setOnClickListener(this)
然后width
必须为null
。这意味着button1
不是layout
文件activity\u main.xml
中按钮的id
检查您的布局
文件是否有类似的按钮
<Button
android:id="@+id/button1"
.../>
如果你觉得你有这个,然后尝试清理你的项目
项目-->清除
如果这不能解决您的问题,请发布该xml文件。但是我相信你会发现你没有。主要活动中的错误,那就是你应该在这里发布的…@Brianj发布你的java代码只是onCreate()
而不是整个类,除非你认为还有其他特别相关的东西。我有七个Java类,但从lo-cat来看,它似乎是主类,所以我将发布它。第27行是什么?并确保所有这些id
s都存在于activity\u main.xml
中。问题在于您在回答中所述的id。按钮名称是大写的button。
<Button
android:id="@+id/button1"
.../>