单击按钮时获取空点异常(Android)

时间:2015-07-23 22:02:16

标签: java android xml

每当我点击Front Page中的AWS按钮时,我就会得到Null点异常,我已经脑筋急转直下,我无法理解为什么。 我将粘贴相关的几行代码,请查看它,并让我知道我哪里出错了。 感谢

清单文件

     <activity
        android:name=".Front"
        android:label="@string/title_activity_front" >
        <intent-filter>
            <action android:name="com.example.getit.Front" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
   <activity
        android:name=".AwsStartAct"
        android:label="@string/title_activity_aws_start" >
        <intent-filter>
            <action android:name="com.example.getit.AwsStartAct" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Front.class

   public class Front extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front);
    //Declaring all the buttons
    Button bAWS = (Button) findViewById(R.id.bAWS);
    Button bAzure = (Button) findViewById(R.id.bMAzure);
    Button bHadoop = (Button) findViewById(R.id.bHadoop);
    //creating on click listeners
    //for aws
    bAWS.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //go to aws option page

            Intent intent= new Intent(Front.this,AwsStartAct.class);
            startActivity(intent);
        }
    });
    //for azure
    bAzure.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //go to azure option page
            Intent intent= new Intent(Front.this,Azurestart.class);
            startActivity(intent);
        }
    });
    //for hadoop
    bHadoop.setOnClickListener(new View.OnClickListener() {

@Override
//this will have all the things that the button Compute will do on click
public void onClick(View v) {
    // TODO Auto-generated method stub

}
 });
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.front, 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);
    }

}

AwsStartAct.class

  public class AwsStartAct extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aws_start);
    //Declaring buttons and linking it with the XML Button by ID
    Button Compute, Storage, Database;
    Compute=(Button) findViewById (R.id.bCompute);
    Storage=(Button) findViewById (R.id.bStorage);
    Database=(Button) findViewById (R.id.bHadoop);
    //Defining onClick listener for each button
    //Compute
    Compute.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //going to the compute page
            Intent intent = new Intent(AwsStartAct.this,awscompute.class);
            startActivity(intent);
        }
    });
    //Storage
    Storage.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
    //Database
    Database.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.aws_start, 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);
}
 }

活动xml布局

   <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"
tools:context="com.example.getit.Front"
android:background="#555" >
  <Button
    android:id="@+id/bAWS"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="132dp"
    android:text="AWS" />

<Button
    android:id="@+id/bMAzure"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bAWS"
    android:layout_below="@+id/bAWS"
    android:layout_marginTop="33dp"
    android:text="Microsoft Azure" />

<Button
    android:id="@+id/bHadoop"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bMAzure"
    android:layout_below="@+id/bMAzure"
    android:layout_marginTop="33dp"
    android:text="Hadoop" />

<TextView
    android:id="@+id/tWelcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="26dp"
    android:gravity="center"
    android:text="@string/Ref"
    android:textColor="#FFF"
    android:textSize="20sp" />

<TextView
    android:id="@+id/frontText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tWelcome"
    android:layout_centerHorizontal="true"
    android:textSize="18dp"
    android:layout_marginTop="16dp"
    android:text="Choose the IT Service provider"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFF" />

  </RelativeLayout>

Aws点击布局

 <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"
tools:context="com.example.getit.AwsStartAct" 
android:background="#555" >


<TextView
    android:id="@+id/tChoose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tWelcome"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="11dp"
    android:text="Choose the type of Service you want"
    android:textColor="#FFF"
    android:textSize="18sp" />

<Button
    android:id="@+id/bCompute"
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tChoose"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:gravity="center"
    android:text="Compute" />

<Button
    android:id="@+id/bStorage"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bCompute"
    android:layout_below="@+id/bCompute"
    android:layout_marginTop="41dp"
    android:text="Storage" />

<Button
    android:id="@+id/bDatabase"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bStorage"
    android:layout_below="@+id/bStorage"
    android:layout_marginTop="37dp"
    android:text="DataBase" />

    </RelativeLayout>

堆栈跟踪

07-23 22:09:28.518: E/AndroidRuntime(361): FATAL EXCEPTION: main
07-23 22:09:28.518: E/AndroidRuntime(361): java.lang.RuntimeException:    Unable to start activity      ComponentInfo{com.example.getit/com.example.getit.AwsStartAct}:    java.lang.NullPointerException
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.os.Looper.loop(Looper.java:123)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-23 22:09:28.518: E/AndroidRuntime(361):  at java.lang.reflect.Method.invokeNative(Native Method)
07-23 22:09:28.518: E/AndroidRuntime(361):  at java.lang.reflect.Method.invoke(Method.java:507)
07-23 22:09:28.518: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-23 22:09:28.518: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-23 22:09:28.518: E/AndroidRuntime(361):  at dalvik.system.NativeStart.main(Native Method)
07-23 22:09:28.518: E/AndroidRuntime(361): Caused by: java.lang.NullPointerException
07-23 22:09:28.518: E/AndroidRuntime(361):  at com.example.getit.AwsStartAct.onCreate(AwsStartAct.java:46)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-23 22:09:28.518: E/AndroidRuntime(361):  ... 11 more

2 个答案:

答案 0 :(得分:3)

AwsStartAct onCreate()中,您使用错误的ID bHadoop作为按钮Database。它不在布局中,因此findViewById()找不到它并返回null。

这就是问题所在:

Database=(Button) findViewById (R.id.bHadoop);

最有可能是:

R.id.bDatabase

答案 1 :(得分:0)

你调用Database =(Button)findViewById(R.id.bHadoop);在名为activity_aws_start的布局中但是在这个布局中不存在任何按钮whit id bHadoop,在这个布局中你命名为bCompute,bCompute和bDatabase这是你的错误尝试Database =(Button)findViewById(R.id.bDatabase);