简体   繁体   English

未使用意图打开活动

[英]Activity Not Opening Using Intent

I am making an app and I need an activity to open up when one of the buttons is pushed. 我正在制作一个应用程序,按下一个按钮时需要打开一个活动。 They all open the same activity. 他们都打开相同的活动。 I've checked, they all have the right file names and they're all in the manifest, but when I try and click the button to open the activity, it tells me "Unfortunately ... has stopped." 我已经检查过,它们都具有正确的文件名,它们都在清单中,但是当我尝试单击该按钮以打开活动时,它告诉我“很不幸……已停止。” Please help me. 请帮我。 Here is the Manifest: 这是清单:

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

Here is the MainActivityWithButtons.java: 这是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);
}

} }

Here is the Activity I'm trying to open: 这是我要打开的活动:

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

} }

ALL help will be greatly appreciated! 所有帮助将不胜感激! Thanks guys 多谢你们

Edit: 编辑:

LogCat: 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)

Locate this: 找到这个:

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

After your setContentView. 在您的setContentView之后。

I think the problem may be in your Manifest. 我认为问题可能出在您的清单上。 You have not set 您尚未设定

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

The system uses this to enable up navigation behavior (which users will expect) and I don't know if you can navigate as you intend without including this in your manifest. 系统使用它来启用导航行为(用户会期望的),并且我不知道是否可以在不将其包含在清单中的情况下按预期进行导航。

To support older devices, also include: 为了支持较旧的设备,还包括:

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

within the body of your activity declaration. 在您的活动声明的正文中。

For more information, see http://developer.android.com/training/basics/firstapp/starting-activity.html . 有关更多信息,请参见http://developer.android.com/training/basics/firstapp/starting-activity.html

I have previously followed the guide at developer.android.com to implement such a function without issue. 之前,我已经按照developer.android.com上的指南实施了这样的功能,没有出现任何问题。

The problem is here: 问题在这里:

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

Which is caused by calling findViewById() before you have a valid Context in your Activity. 这是由于在Activity中具有有效的Context之前调用findViewById()引起的。

When you call findViewById() , it's actually short for this.findViewById() , and in your case, this is null, hence the NullPointerException. 当您调用findViewById() ,它实际上是this.findViewById() ,在您的情况下, this为null,因此为NullPointerException。

Note that in order to find the Buttons, the correct layout needs to be inflated into the window, so you need to place these calls after setContentView() as well: 请注意,为了找到Button,需要将正确的布局放大到窗口中,因此您还需要将这些调用放在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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM