使用Intent

时间:2015-07-18 17:51:27

标签: java android android-intent

我正在创建一个应用程序,当按下其中一个按钮时,我需要一个活动来打开。他们都开了同样的活动。我已经检查过,它们都有正确的文件名,它们都在清单中,但是当我尝试单击按钮打开活动时,它会告诉我“不幸......已经停止了”。请帮我。这是清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.apps.testp">

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.apps.testp.MainActivityWithButtons"
        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.apps.testp.LocationSelectFromAlertButtons"
        android:label="@string/title_activity_location_select_from_alert_buttons" >
    </activity>

</application>

这是MainActivityWithButtons.java:

package com.apps.testp;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


@SuppressWarnings("deprecation")
public class MainActivityWithButtons extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity_with_buttons);

    Button breakIn=(Button)findViewById(R.id.houseBreakIn);
    Button robbery=(Button)findViewById(R.id.storeRobbery);
    Button car=(Button)findViewById(R.id.carTheft);
    Button assaulting=(Button)findViewById(R.id.assault);
    Button shots=(Button)findViewById(R.id.shotsFired);
    Button fight=(Button)findViewById(R.id.fighting);
    Button address=(Button)findViewById(R.id.setOrChangeAddress);


    address.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
            startActivity(i);

        }
    });



    breakIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
            startActivity(i);
        }
    });



    robbery.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
                startActivity(i);
                }
            });



    car.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
            startActivity(i);
        }
    });


    assaulting.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
            startActivity(i);
        }
    });


    shots.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
            startActivity(i);
        }
    });


    fight.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
            startActivity(i);
        }
    });



    //Next Listener Goes Here
}


@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_activity_with_buttons, 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);
}

}

以下是我要打开的活动:

package com.apps.testp;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    @SuppressWarnings("deprecation")
    public class LocationSelectFromAlertButtons extends ActionBarActivity {

DBAdapter myDB;

Button save=(Button)findViewById(R.id.saveBtn);
TextView currentAddress=(TextView)findViewById(R.id.CurrAddress);
EditText addressNew=(EditText)findViewById(R.id.tbNewAddress);

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_select_from_alert_buttons);

    openDB();



}
private void openDB() {
    myDB = new DBAdapter(this);
    myDB.open();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    closeDB();
}

private void closeDB() {

    myDB.close();
}
public void saveButtonClicked(){
    long newId =myDB.insertRow(LocationSelectFromAlertButtons.this.addressNew.getText().toString());


}








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

}

所有帮助将不胜感激! 谢谢你们

修改

logcat的:

    07-18 20:01:55.572  21821-21821/com.apps.testp D/dalvikvm? Late-enabling CheckJNI
07-18 20:01:55.572  21821-21821/com.apps.testp D/dalvikvm? Try to disable coredump for pid 21821
07-18 20:01:55.903  21821-21821/com.apps.testp I/dalvikvm? Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-18 20:01:55.903  21821-21821/com.apps.testp W/dalvikvm? VFY: unable to resolve virtual method 411: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-18 20:01:55.903  21821-21821/com.apps.testp D/dalvikvm? VFY: replacing opcode 0x6e at 0x0002
07-18 20:01:55.903  21821-21821/com.apps.testp I/dalvikvm? Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-18 20:01:55.903  21821-21821/com.apps.testp W/dalvikvm? VFY: unable to resolve virtual method 433: Landroid/content/res/TypedArray;.getType (I)I
07-18 20:01:55.903  21821-21821/com.apps.testp D/dalvikvm? VFY: replacing opcode 0x6e at 0x0002
07-18 20:01:56.112  21821-21821/com.apps.testp D/libEGL? loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
07-18 20:01:56.143  21821-21821/com.apps.testp D/libEGL? loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
07-18 20:01:56.153  21821-21821/com.apps.testp D/libEGL? loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
07-18 20:01:56.233  21821-21821/com.apps.testp D/OpenGLRenderer? Enabling debug mode 0
07-18 20:01:58.432  21821-21821/com.apps.testp D/AndroidRuntime? Shutting down VM
07-18 20:01:58.432  21821-21821/com.apps.testp W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x41abae10)
07-18 20:01:58.432  21821-21821/com.apps.testp E/AndroidRuntime? FATAL EXCEPTION: main
            java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.apps.testp/com.apps.testp.LocationSelectFromAlertButtons}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
            at android.app.ActivityThread.access$700(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5279)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
             Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1886)
            at com.apps.testp.LocationSelectFromAlertButtons.<init>(LocationSelectFromAlertButtons.java:17)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
            at android.app.ActivityThread.access$700(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5279)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

找到这个:

Button save=(Button)findViewById(R.id.saveBtn);
TextView currentAddress=(TextView)findViewById(R.id.CurrAddress);
EditText addressNew=(EditText)findViewById(R.id.tbNewAddress);

在你的setContentView之后。

答案 1 :(得分:0)

我认为问题可能在你的清单中。你还没有设置

android:parentActivityName="com.apps.testp.MainActivityWithButtons"

系统使用它来启用导航行为(用户会期望)并且我不知道您是否可以按照您的意图导航而不将其包含在清单中。

要支持旧设备,还包括:

<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="com.apps.testp.MainActivityWithButtons" />

在您的活动声明正文中。

有关详细信息,请参阅http://developer.android.com/training/basics/firstapp/starting-activity.html

我之前已经按照developer.android.com上的指南来实现这样的功能而没有问题。

答案 2 :(得分:0)

问题在于:

Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1886)

在您的活动中拥有有效的上下文之前,调用findViewById()会导致这种情况。

当您致电findViewById()时,它实际上是this.findViewById()的缩写,在您的情况下,this为空,因此为NullPointerException。

请注意,为了找到按钮,需要将正确的布局扩展到窗口中,因此您需要在setContentView()之后放置这些调用:

Button save;
TextView currentAddress;
EditText addressNew;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_select_from_alert_buttons);

    save = (Button)findViewById(R.id.saveBtn);
    currentAddress = (TextView)findViewById(R.id.CurrAddress);
    addressNew = (EditText)findViewById(R.id.tbNewAddress);

    openDB();

}