空指针异常。无法运行我的Android应用程序

时间:2014-04-10 17:43:49

标签: android eclipse nullpointerexception

请帮我修复我的Android应用。当我在模拟器中运行它时会发生这种情况。似乎找不到这些错误的答案。

这是我的logcat

04-10 17:37:56.283: E/AndroidRuntime(2177): FATAL EXCEPTION: main
04-10 17:37:56.283: E/AndroidRuntime(2177): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miraapp/com.example.miraapp.MainActivity}: java.lang.NullPointerException
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.os.Looper.loop(Looper.java:137)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at java.lang.reflect.Method.invoke(Method.java:511)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at dalvik.system.NativeStart.main(Native Method)
04-10 17:37:56.283: E/AndroidRuntime(2177): Caused by: java.lang.NullPointerException
04-10 17:37:56.283: E/AndroidRuntime(2177):     at com.example.miraapp.MainActivity.onCreate(MainActivity.java:30)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.Activity.performCreate(Activity.java:5104)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 17:37:56.283: E/AndroidRuntime(2177):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 17:37:56.283: E/AndroidRuntime(2177):     ... 11 more

这是我的主要活动

package com.example.miraapp;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

 public class MainActivity extends Activity implements OnClickListener
{


Button button1;
EditText etResponse;
    TextView tvIsConnected;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this); 


}

private void button1Click()
{
    startActivity(new Intent(this,GUI.class));  
}
public void onClick(View v)
{

    switch (v.getId())
    {
        case R.id.button1:
            button1Click();
            break;
        case R.id.button2:
            button2Click();
            break;
    }
}

private void button2Click()
{


}

};

这是我的manifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.miraapp"
android:versionCode="1"
android:versionName="1.0"
 >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17"

 />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.miraapp.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="com.example.miraapp.GUI"
        android:label="@string/app_name" >
    </activity>

</application>

这是我的fragment_main

<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"
android:background="#000000"
tools:context="com.example.miraapp.MainActivity$PlaceholderFragment" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="131dp"
    android:text="@string/START_fix" 
    android:onClick="button1Click"/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp"
    android:contentDescription="@string/title_fix"
    android:src="@drawable/mira4" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="63dp"
    android:text="@string/CONNECT_fix" />

 </RelativeLayout>

3 个答案:

答案 0 :(得分:3)

该按钮属于fragment_main.xml而非activity_main.xml。因此,按钮的初始化失败,导致NullPointerExcpetion

初始化onCreateView Fragment中的按钮。我的代码中没有fragment

所以只需改变这个

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

答案 1 :(得分:1)

请更正:

setContentView(R.layout.activity_main)

您的布局名称是fragment_main

答案 2 :(得分:0)

fragment_main中的整个代码复制到activity_main ..

或者

更改此行

setContentView(R.layout.activity_main)

setContentView(R.layout.fragment_main)

解决问题的方法..