为什么Android App在尝试启动新活动时崩溃

时间:2013-11-29 18:33:35

标签: android android-intent onclicklistener

所以我正在尝试将应用程序设计为以意图进入另一个屏幕,但它会一直崩溃。

我正在使用带有点击监听器的imageButton。我为目标页面添加了第二个java模块,一个布局并将应用程序添加到清单文件中。如果您希望我提供更多信息,请告诉我

Logcat错误

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bet/com.example.bet.MainActivity}: java.lang.NullPointerException

     Caused by: java.lang.NullPointerException
        at com.example.bet.MainActivity.onCreate(MainActivity.java:42)

我注意到哪一行是42

主要活动主要代码已更新

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;

import android.view.ViewGroup;
import android.os.Build;

import android.view.View;
import android.content.Intent;  //Launching other pages
import android.app.Activity; // Activity Launcher
import android.widget.ImageButton; //Image buttons
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {

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

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


        // / / / / / / / / / / / / / / / / / / / / / /
        //Initialize forms of the ImageButtons
        // / / / / / / / / / / / / / / / / / / / / / /
        /* Forms Button */
        ImageButton pcaButton = (ImageButton)findViewById(R.id.pcaButton);

        // / / / / / / / / / / / / / / / / / / / / / /
        //Attach the Listener (On Click Event handler)
        // / / / / / / / / / / / / / / / / / / / / / /

        pcaButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this , pcaActivity.class);
                startActivity(intent);
            }
        });

        return rootView;
    }
}

}

清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />

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

String.xml

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">bet</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_pca">pca</string>

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

fragment_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:background="@drawable/abc_ab_transparent_light_holo">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">

    <ImageButton
        android:id="@+id/pcaButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/pca1"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

4 个答案:

答案 0 :(得分:2)

由于片段中定义了ImageButton,因此您需要在片段中的onCreateView内找到它(因为它在活动中不存在)。

PlaceholderFragment中,执行以下操作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    // / / / / / / / / / / / / / / / / / / / / / /
    //Initialize forms of the ImageButtons
    // / / / / / / / / / / / / / / / / / / / / / /
    /* Button */
    ImageButton pcaButton = (ImageButton)view.findViewById(R.id.pcaButton);

    // / / / / / / / / / / / / / / / / / / / / / /
     //Attach the Listener (On Click Event handler)
    // / / / / / / / / / / / / / / / / / / / / / /
    pcaButton.setOnClickListener( new View.OnClickListener() {
        @Override                                 //<-- Line 42
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this , pcaActivity.class);
            startActivity(intent);
        }
    });

    return view;
}

答案 1 :(得分:0)

在你的布局中:

<ImageButton
    android:id="@+id/pcaButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pca1"
    android:layout_gravity="center_horizontal"
    android:on="center_horizontal"
    android:onClick="some_function"
    />

在你的班级

public class MainActivity extends ActionBarActivity {
   public void some_function(View v){
       //Do what you want to do
       Intent intent = new Intent(MainActivity.this , pcaActivity.class);
       startActivity(intent);
   }

这永远不会失败,我首先要保护一个OnclickListener并且永远不会失败

答案 2 :(得分:0)

我遇到了几乎相同的问题,但默认情况下PlaceholderFragmentstatic并且无法让我使用MainActivity.this,因为它不是static,即使我找到了一种使用它的方法,它向我展示了NullpointerException。尝试使用MainActivity.this更改getActivity()

答案 3 :(得分:-2)

public class MainActivity extends ActionBarActivity

可能会扩展Activity而不是ActionBarActivity?