为了账号安全,请及时绑定邮箱和手机立即绑定

为何不能加载点击事件,只要打开点击事件,及时里面内容为空,也会出错,代码错在哪里?

package com.giawei.fragementdemo;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
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;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	private Button button1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1=(Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			//	Intent intent=new Intent(MainActivity.this,MainActivity2.class);
			//	startActivity(intent);
				
			}
		});

		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;
		}
	}

}


正在回答

4 回答

09-17 11:46:34.169: D/AndroidRuntime(1528): Shutting down VM

09-17 11:46:34.169: W/dalvikvm(1528): threadid=1: thread exiting with uncaught exception (group=0xb2a4dba8)

09-17 11:46:34.189: E/AndroidRuntime(1528): FATAL EXCEPTION: main

09-17 11:46:34.189: E/AndroidRuntime(1528): Process: com.giawei.fragementdemo, PID: 1528

09-17 11:46:34.189: E/AndroidRuntime(1528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.giawei.fragementdemo/com.giawei.fragementdemo.MainActivity}: java.lang.NullPointerException

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.ActivityThread.access$800(ActivityThread.java:135)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.os.Handler.dispatchMessage(Handler.java:102)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.os.Looper.loop(Looper.java:136)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.ActivityThread.main(ActivityThread.java:5017)

09-17 11:46:34.189: E/AndroidRuntime(1528): at java.lang.reflect.Method.invokeNative(Native Method)

09-17 11:46:34.189: E/AndroidRuntime(1528): at java.lang.reflect.Method.invoke(Method.java:515)

09-17 11:46:34.189: E/AndroidRuntime(1528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

09-17 11:46:34.189: E/AndroidRuntime(1528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

09-17 11:46:34.189: E/AndroidRuntime(1528): at dalvik.system.NativeStart.main(Native Method)

09-17 11:46:34.189: E/AndroidRuntime(1528): Caused by: java.lang.NullPointerException

09-17 11:46:34.189: E/AndroidRuntime(1528): at com.giawei.fragementdemo.MainActivity.onCreate(MainActivity.java:25)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.Activity.performCreate(Activity.java:5231)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

09-17 11:46:34.189: E/AndroidRuntime(1528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

09-17 11:46:34.189: E/AndroidRuntime(1528): ... 11 more

09-17 11:47:08.179: I/Process(1528): Sending signal. PID: 1528 SIG: 9

09-17 11:51:35.439: D/AndroidRuntime(1575): Shutting down VM

09-17 11:51:35.439: W/dalvikvm(1575): threadid=1: thread exiting with uncaught exception (group=0xb2a4dba8)

09-17 11:51:35.449: E/AndroidRuntime(1575): FATAL EXCEPTION: main

09-17 11:51:35.449: E/AndroidRuntime(1575): Process: com.giawei.fragementdemo, PID: 1575

09-17 11:51:35.449: E/AndroidRuntime(1575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.giawei.fragementdemo/com.giawei.fragementdemo.MainActivity}: java.lang.NullPointerException

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.ActivityThread.access$800(ActivityThread.java:135)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.os.Handler.dispatchMessage(Handler.java:102)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.os.Looper.loop(Looper.java:136)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.ActivityThread.main(ActivityThread.java:5017)

09-17 11:51:35.449: E/AndroidRuntime(1575): at java.lang.reflect.Method.invokeNative(Native Method)

09-17 11:51:35.449: E/AndroidRuntime(1575): at java.lang.reflect.Method.invoke(Method.java:515)

09-17 11:51:35.449: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

09-17 11:51:35.449: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

09-17 11:51:35.449: E/AndroidRuntime(1575): at dalvik.system.NativeStart.main(Native Method)

09-17 11:51:35.449: E/AndroidRuntime(1575): Caused by: java.lang.NullPointerException

09-17 11:51:35.449: E/AndroidRuntime(1575): at com.giawei.fragementdemo.MainActivity.onCreate(MainActivity.java:31)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.Activity.performCreate(Activity.java:5231)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

09-17 11:51:35.449: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

09-17 11:51:35.449: E/AndroidRuntime(1575): ... 11 more

09-17 11:51:40.299: I/Process(1575): Sending signal. PID: 1575 SIG: 9


0 回复 有任何疑惑可以回复我~

出错显示 空指针,但是不知道在哪里出错

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第二门课(第1季)
  • 参与学习       111172    人
  • 解答问题       1457    个

本课程由浅入深地带您学会Android的常用控件的开发和使用

进入课程

为何不能加载点击事件,只要打开点击事件,及时里面内容为空,也会出错,代码错在哪里?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信