我在通过使用按钮开始新活动时遇到问题。
我已经检查了与我的问题相关的先前问题,其中一个几乎相同,但解决方案对我来说并不奏效。所以这是我的问题。
我有 2 个活动,一个叫 Acilis,第二个 MainActivity,
因此,当应用程序启动时,Acilis.class 启动并且那里有两个按钮,它假设在单击按钮后启动 MainActivity.class。单击应用程序崩溃后。这是我的代码
显现:
<activity
android:name="com.example.workbench.Acilis"
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="com.example.workbench.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.workbench.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Acilis类
public class Acilis extends Activity{
Button erkek, kadin;
Intent intent = new Intent("com.example.workbench.MAINACTIVITY");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anasayfa);
erkek = (Button) findViewById(R.id.button_erkek);
kadin = (Button) findViewById(R.id.button_kadin);
erkek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(intent);
setContentView(R.layout.activity_main);
}
});
kadin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(intent);
setContentView(R.layout.activity_main);
}
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
sayfa_no = 0;
ileri = (ImageButton) findViewById(R.id.button_ileri);
geri = (ImageButton) findViewById(R.id.button_geri);
fav = (ImageButton) findViewById(R.id.button_fav);
ileri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sayfa_no++;
sayfa.setText(sayfa_no + "/100");
}
});
geri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sayfa_no--;
sayfa.setText(sayfa_no + "/100");
}
});
}
@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;
}
我已经盖好了括号,不用担心,Eclipse 不会给出任何错误。我认为我遇到了语义错误。
日志猫:
06-17 10:59:41.369: E/AndroidRuntime(15450): FATAL EXCEPTION: main
06-17 10:59:41.369: E/AndroidRuntime(15450): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workbench/com.example.workbench.MainActivity}: java.lang.NullPointerException
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.os.Looper.loop(Looper.java:137)
06-17 10:59:41.369: E/AndroidRuntime(15450): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-17 10:59:41.369: E/AndroidRuntime(15450): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 10:59:41.369: E/AndroidRuntime(15450): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 10:59:41.369: E/AndroidRuntime(15450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
它的错误案例太长,发布了一些部分。
谢谢你们!