我在Android应用程序中的主要活动有些问题。该应用无法打开。请帮助。感谢
package com.example.sgdriverdiary;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button button2, button3, button4, button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button1 = (Button) findViewById(R.id.button1);
button2.setOnClickListener((OnClickListener) this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button1.setOnClickListener(this);
//if (savedInstanceState == null) {
//getSupportFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
//}
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
/*public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}*/
/*public void rate(View view){
Intent intent = new Intent(this, Rate.class);
startActivity(intent);
}
public void cal(View view){
Intent intent = new Intent(this, Cal.class);
startActivity(intent);
}
public void search(View view){
Intent intent = new Intent(this,Search.class);
startActivity(intent);
}*/
public void onClick(View v){
Intent intent = new Intent();
if(v == button3)
{
intent.setClass(this, Cal.class);
}
else if(v == button1)
{
intent.setClass(this, Rate.class);
}
else if(v == button4)
{
intent.setClass(this, Search.class);
}
/*else if(v== button2)
{
intent.setClass(this, Record.class);
}*/
else{
return;
}
startActivity(intent);
}
}
这是log cat
05-18 12:28:11.753: E/AndroidRuntime(1048): FATAL EXCEPTION: main
05-18 12:28:11.753: E/AndroidRuntime(1048): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sgdriverdiary/com.example.sgdriverdiary.MainActivity}: java.lang.NullPointerException
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.os.Looper.loop(Looper.java:137)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-18 12:28:11.753: E/AndroidRuntime(1048): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 12:28:11.753: E/AndroidRuntime(1048): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 12:28:11.753: E/AndroidRuntime(1048): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 12:28:11.753: E/AndroidRuntime(1048): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 12:28:11.753: E/AndroidRuntime(1048): at dalvik.system.NativeStart.main(Native Method)
05-18 12:28:11.753: E/AndroidRuntime(1048): Caused by: java.lang.NullPointerException
05-18 12:28:11.753: E/AndroidRuntime(1048): at com.example.sgdriverdiary.MainActivity.onCreate(MainActivity.java:25)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.Activity.performCreate(Activity.java:4465)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-18 12:28:11.753: E/AndroidRuntime(1048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-18 12:28:11.753: E/AndroidRuntime(1048): ... 11 more
05-18 12:28:11.913: I/dalvikvm(1048): threadid=3: reacting to signal 3
05-18 12:28:11.943: I/dalvikvm(1048): Wrote stack traces to '/data/anr/traces.txt'
05-18 12:28:12.284: I/dalvikvm(1048): threadid=3: reacting to signal 3
05-18 12:28:12.294: I/dalvikvm(1048): Wrote stack traces to '/data/anr/traces.txt'
05-18 12:33:11.793: I/Process(1048): Sending signal. PID: 1048 SIG: 9
答案 0 :(得分:1)
不要那样使用onClick
- 而且您不需要实施onClickListener
。
在您的XML文件中,您可以设置此属性:android:onClick="whatever"
,然后您不需要所有setOnClickListener
内容。只需使用
public void whatever (View v){
//do your stuff
}
答案 1 :(得分:0)
试试这个:
如果您的button = (Button) findViewById(R.id.button2);
然后setOnClickListener()
为
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
这是在xml中设置android:onClick
侦听器的首选方法。