使用startActivityForResult时应用程序崩溃

时间:2014-04-04 15:01:47

标签: java android xml eclipse

我在2个类之间使用startActivityForResult" MainActivity" &安培; " ABC"上课。我遇到的问题是应用程序在启动时不断崩溃,并且通过阅读LogCat我认为由于NPE而导致崩溃" NullPointException"在MainActivity类中(但我不确定)。我已经搜索了几个小时的解决方案,但无法找到解决我问题的方法。

1)MainActivity类:

package com.example.passstrings;


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
Button bt1;
TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      tv2 = (TextView) findViewById(R.id.textView2);
      bt1=(Button) findViewById(R.id.button1);
    bt1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this , abc.class);
                startActivityForResult(intent , 0);

            }
    });



        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

@Override
protected void onActivityResult(int requestCode , int resultCode , Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
        Intent intent = getIntent();
        String pass= intent.getExtras().getString("key");
        tv2.setText(pass);
        }

}

    @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 static 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);
            return rootView;
        }
    }

}

2)abc class

package com.example.passstrings;

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

public class abc extends Activity{
TextView tv1;
String setData;
RadioButton r1,r2,r3,r4;
Button bt2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abc);
        r1 = (RadioButton) findViewById(R.id.radioButton1);
        r2 = (RadioButton) findViewById(R.id.RadioButton01);
        r3 = (RadioButton) findViewById(R.id.RadioButton02);
        r4 = (RadioButton) findViewById(R.id.RadioButton03);
        bt2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 if (r1.isChecked() || r2.isChecked() || r3.isChecked() == true){
                        setData ="Failed";
                    }else if (r3.isChecked() ==true){
                        setData="Passed";
                    }else{
                        setData="You didn't choose an answer";
                    }
                    Intent Int = new Intent(abc.this , MainActivity.class);
                    Int.putExtra("key", setData);
                    setResult(RESULT_OK , Int);
                     finish();






                        }

});
    }
}

3)LogCat:

04-04 14:52:15.406: I/dalvikvm(369): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 14:52:15.415: W/dalvikvm(369): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 14:52:15.415: D/dalvikvm(369): VFY: replacing opcode 0x6e at 0x008b
04-04 14:52:15.415: I/dalvikvm(369): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 14:52:15.415: W/dalvikvm(369): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 14:52:15.415: D/dalvikvm(369): VFY: replacing opcode 0x6e at 0x0099
04-04 14:52:15.425: D/dalvikvm(369): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 14:52:15.425: D/dalvikvm(369): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 14:52:15.535: D/AndroidRuntime(369): Shutting down VM
04-04 14:52:15.535: W/dalvikvm(369): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 14:52:15.545: E/AndroidRuntime(369): FATAL EXCEPTION: main
04-04 14:52:15.545: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.MainActivity}: java.lang.NullPointerException
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.os.Looper.loop(Looper.java:123)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 14:52:15.545: E/AndroidRuntime(369):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:52:15.545: E/AndroidRuntime(369):  at java.lang.reflect.Method.invoke(Method.java:521)
04-04 14:52:15.545: E/AndroidRuntime(369):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 14:52:15.545: E/AndroidRuntime(369):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 14:52:15.545: E/AndroidRuntime(369):  at dalvik.system.NativeStart.main(Native Method)
04-04 14:52:15.545: E/AndroidRuntime(369): Caused by: java.lang.NullPointerException
04-04 14:52:15.545: E/AndroidRuntime(369):  at com.example.passstrings.MainActivity.onCreate(MainActivity.java:26)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 14:52:15.545: E/AndroidRuntime(369):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 14:52:15.545: E/AndroidRuntime(369):  ... 11 more

4)abc.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">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:text="Which of the following vitamins helps clotting the blood : "
        android:textSize="30sp" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginLeft="62dp"
        android:layout_marginTop="14dp"
        android:text="Vitamin A" />

    <RadioButton
        android:id="@+id/RadioButton03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/RadioButton01"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp"
        android:text="None Of Above" />

    <RadioButton
        android:id="@+id/RadioButton01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/RadioButton03"
        android:layout_alignRight="@+id/RadioButton02"
        android:layout_marginBottom="26dp"
        android:text="Vitamin C" />

    <RadioButton
        android:id="@+id/RadioButton02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioButton1"
        android:layout_below="@+id/radioButton1"
        android:layout_marginTop="16dp"
        android:text="Vitamin D" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Check"
        android:textSize="30sp" />

</RelativeLayout>

5)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: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.ytdownload.MainActivity$PlaceholderFragment" >

    >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:text="Click Below to start the test . Please only choose on of the answers"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp"
        android:text="Start"
        android:textSize="30sp" />

</RelativeLayout>

6)清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        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.passstrings.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.passstrings.abc" />
    </application>

</manifest>

7)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:ignore="MergeRootFrame" 

    tools:context="com.example.passstrings.MainActivity$PlaceholderFragment" >

    >


</FrameLayout>

如果我将MainActivity中的setContentView更改为片段,它会在LogCat&#34中向我显示此错误;虽然将其设置为activity_main并且编辑fragment_main总是让我感觉很好并且#34;

04-04 15:13:42.995: I/dalvikvm(592): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 15:13:42.995: W/dalvikvm(592): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 15:13:42.995: D/dalvikvm(592): VFY: replacing opcode 0x6e at 0x008b
04-04 15:13:42.995: I/dalvikvm(592): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 15:13:42.995: W/dalvikvm(592): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 15:13:42.995: D/dalvikvm(592): VFY: replacing opcode 0x6e at 0x0099
04-04 15:13:43.006: D/dalvikvm(592): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 15:13:43.006: D/dalvikvm(592): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 15:13:43.135: E/FragmentManager(592): No view found for id 0x7f050042 (com.example.passstrings:id/container) for fragment PlaceholderFragment{4a310490 #0 id=0x7f050042}
04-04 15:13:43.135: E/FragmentManager(592): Activity state:
04-04 15:13:43.135: D/FragmentManager(592):   Local FragmentActivity 4a2f4ae8 State:
04-04 15:13:43.135: D/FragmentManager(592):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
04-04 15:13:43.135: D/FragmentManager(592):     mLoadersStarted=false
04-04 15:13:43.135: D/FragmentManager(592):   Active Fragments in 4a2f5aa8:
04-04 15:13:43.135: D/FragmentManager(592):     #0: PlaceholderFragment{4a310490 #0 id=0x7f050042}
04-04 15:13:43.151: D/FragmentManager(592):       mFragmentId=#7f050042 mContainerId=#7f050042 mTag=null
04-04 15:13:43.151: D/FragmentManager(592):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
04-04 15:13:43.155: D/FragmentManager(592):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
04-04 15:13:43.155: D/FragmentManager(592):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
04-04 15:13:43.155: D/FragmentManager(592):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
04-04 15:13:43.155: D/FragmentManager(592):       mFragmentManager=FragmentManager{4a2f5aa8 in MainActivity{4a2f4ae8}}
04-04 15:13:43.155: D/FragmentManager(592):       mActivity=com.example.passstrings.MainActivity@4a2f4ae8
04-04 15:13:43.155: D/FragmentManager(592):   Added Fragments:
04-04 15:13:43.165: D/FragmentManager(592):     #0: PlaceholderFragment{4a310490 #0 id=0x7f050042}
04-04 15:13:43.165: D/FragmentManager(592):   FragmentManager misc state:
04-04 15:13:43.165: D/FragmentManager(592):     mActivity=com.example.passstrings.MainActivity@4a2f4ae8
04-04 15:13:43.165: D/FragmentManager(592):     mContainer=android.support.v4.app.FragmentActivity$2@4a2f63b0
04-04 15:13:43.165: D/FragmentManager(592):     mCurState=2 mStateSaved=false mDestroyed=false
04-04 15:13:43.165: D/FragmentManager(592):   View Hierarchy:
04-04 15:13:43.165: D/FragmentManager(592):     com.android.internal.policy.impl.PhoneWindow$DecorView{4a2f9690 V.E..... ... 0,0-0,0}
04-04 15:13:43.175: D/FragmentManager(592):       android.widget.FrameLayout{4a2f9fd0 V.ED.... ... 0,0-0,0}
04-04 15:13:43.175: D/FragmentManager(592):         android.widget.LinearLayout{4a2facf0 V.E..... ... 0,0-0,0}
04-04 15:13:43.175: D/FragmentManager(592):           android.support.v7.internal.widget.ActionBarContainer{4a2fb788 V.ED.... ... 0,0-0,0 #7f050021 app:id/action_bar_container}
04-04 15:13:43.175: D/FragmentManager(592):             android.support.v7.internal.widget.ActionBarView{4a304150 V.E..... ... 0,0-0,0 #7f050022 app:id/action_bar}
04-04 15:13:43.175: D/FragmentManager(592):               android.widget.LinearLayout{4a308a98 G.....C. ... 0,0-0,0}
04-04 15:13:43.175: D/FragmentManager(592):                 android.widget.ImageView{4a3093e8 G.ED.... ... 0,0-0,0 #7f050027 app:id/up}
04-04 15:13:43.175: D/FragmentManager(592):                 android.widget.LinearLayout{4a309618 V.E..... ... 0,0-0,0}
04-04 15:13:43.187: D/FragmentManager(592):                   android.widget.TextView{4a309a98 V.ED.... ... 0,0-0,0 #7f050028 app:id/action_bar_title}
04-04 15:13:43.187: D/FragmentManager(592):                   android.widget.TextView{4a30acd8 G.ED.... ... 0,0-0,0 #7f050029 app:id/action_bar_subtitle}
04-04 15:13:43.187: D/FragmentManager(592):               android.support.v7.internal.widget.ActionBarView$HomeView{4a305088 VFE...C. ... 0,0-0,0}
04-04 15:13:43.187: D/FragmentManager(592):                 android.widget.ImageView{4a307388 G.ED.... ... 0,0-0,0 #7f050027 app:id/up}
04-04 15:13:43.187: D/FragmentManager(592):                 android.widget.ImageView{4a3078e8 V.ED.... ... 0,0-0,0 #7f050014 app:id/home}
04-04 15:13:43.198: D/FragmentManager(592):             android.support.v7.internal.widget.ActionBarContextView{4a30b6a8 G.E..... ... 0,0-0,0 #7f050023 app:id/action_context_bar}
04-04 15:13:43.198: D/FragmentManager(592):           android.widget.FrameLayout{4a30c040 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
04-04 15:13:43.198: D/FragmentManager(592):             android.widget.RelativeLayout{4a30d688 V.E..... ... 0,0-0,0}
04-04 15:13:43.205: D/FragmentManager(592):               android.widget.TextView{4a30db18 V.ED.... ... 0,0-0,0 #7f05001a app:id/textView2}
04-04 15:13:43.205: D/FragmentManager(592):               android.widget.Button{4a30e368 VFED..C. ... 0,0-0,0 #7f05001f app:id/button1}
04-04 15:13:43.205: D/FragmentManager(592):           android.support.v7.internal.widget.ActionBarContainer{4a30c2e8 G.ED.... ... 0,0-0,0 #7f050024 app:id/split_action_bar}
04-04 15:13:43.205: D/AndroidRuntime(592): Shutting down VM
04-04 15:13:43.205: W/dalvikvm(592): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 15:13:43.215: E/AndroidRuntime(592): FATAL EXCEPTION: main
04-04 15:13:43.215: E/AndroidRuntime(592): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f050042 (com.example.passstrings:id/container) for fragment PlaceholderFragment{4a310490 #0 id=0x7f050042}
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.os.Looper.loop(Looper.java:123)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 15:13:43.215: E/AndroidRuntime(592):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 15:13:43.215: E/AndroidRuntime(592):  at java.lang.reflect.Method.invoke(Method.java:521)
04-04 15:13:43.215: E/AndroidRuntime(592):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 15:13:43.215: E/AndroidRuntime(592):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 15:13:43.215: E/AndroidRuntime(592):  at dalvik.system.NativeStart.main(Native Method)
04-04 15:13:43.215: E/AndroidRuntime(592): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f050042 (com.example.passstrings:id/container) for fragment PlaceholderFragment{4a310490 #0 id=0x7f050042}
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.Activity.performStart(Activity.java:3781)
04-04 15:13:43.215: E/AndroidRuntime(592):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
04-04 15:13:43.215: E/AndroidRuntime(592):  ... 11 more

2 个答案:

答案 0 :(得分:2)

NullPointerException是因为您试图在activity_main.xml中找到R.id.textView2R.id.button1,而那些ID不存在。

如果仔细查看日志中有一行说明NPE发生的位置,在这种情况下是这样的:

04-04 14:52:15.545: E/AndroidRuntime(369): Caused by: java.lang.NullPointerException
04-04 14:52:15.545: E/AndroidRuntime(369):  at com.example.passstrings.MainActivity.onCreate(MainActivity.java:26)

正如您所看到的,在类MainActivity.java的第26行中,特别是在onCreate()方法中发生错误的地方,我几乎可以肯定错误是由缺少来自xml的id。要解决此问题,您必须将ButtonTextView放在main_activity.xml中,并在代码中指定ID(R.id.textView2R.id.button1

答案 1 :(得分:0)

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="10.2332in" height="11.252161in" viewBox="0 0 920.98797 1012.6946" id="svg2" version="1.1" inkscape:version="0.91 r13725" sodipodi:docname="map.svg">
    <defs id="defs4">
        <linearGradient inkscape:collect="always" id="linearGradient6146">
            <stop style="stop-color:#f2f2f2;stop-opacity:1;" offset="0" id="stop6148" />
            <stop style="stop-color:#f2f2f2;stop-opacity:0;" offset="1" id="stop6150" />
        </linearGradient>
        <linearGradient id="linearGradient6140" osb:paint="solid">
            <stop style="stop-color:#000000;stop-opacity:1;" offset="0" id="stop6142" />
        </linearGradient>
        <linearGradient inkscape:collect="always" xlink:href="#linearGradient6146" id="linearGradient6152" x1="757.7793" y1="1019.3268" x2="1032.8184" y2="1019.3268" gradientUnits="userSpaceOnUse" />
        <linearGradient inkscape:collect="always" xlink:href="#linearGradient6146" id="linearGradient6154" gradientUnits="userSpaceOnUse" x1="757.7793" y1="1019.3268" x2="1032.8184" y2="1019.3268" />
    </defs>
    <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.41986485" inkscape:cx="665.09998" inkscape:cy="547.70001" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" units="in" fit-margin-left="-2" fit-margin-right="0" fit-margin-top="-1" fit-margin-bottom="0" inkscape:window-width="1366" inkscape:window-height="705" inkscape:window-x="-8" inkscape:window-y="-8" inkscape:window-maximized="1" width="7.5in" height="9000in">
        <inkscape:grid type="xygrid" id="grid3336" originx="-49.686342" originy="-348.02656" />
    </sodipodi:namedview>
    <metadata id="metadata7">
        <rdf:RDF>
            <cc:Work rdf:about="">
                <dc:format>image/svg+xml</dc:format>
                <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
                <dc:title>
                </dc:title>
            </cc:Work>
        </rdf:RDF>
    </metadata>
    <g inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1" transform="translate(141.31935,-164.56211)">
        <ellipse style="fill:#000000;fill-rule:evenodd;stroke:#000000;stroke-width:1.47842932px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path3349" cx="179.14752" cy="213.21657" rx="21.40518" ry="14.970306" />
        <ellipse style="fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.47842932px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path3349-5" cx="178.278" cy="548.51111" rx="21.40518" ry="14.970306" />
        <path style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1.18673158px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" d="m 178.10765,231.18477 c -2.42669,297.78248 -2.42669,297.78248 -2.42669,297.78248 l 0,-1.11949 2.42669,1.11949 0,0" id="path4175" inkscape:connector-curvature="0" />
        <rect style="fill:#ffffff;fill-opacity:1" id="rect4182" width="157.72238" height="44.910931" x="-20.258638" y="180.71774" />
        <rect style="fill:#ffffff;fill-opacity:1" id="rect4184" width="76.607979" height="37.4258" x="175.63919" y="74.562111" />
        <rect style="fill:#ffffff;fill-opacity:1" id="rect4186" width="135.19063" height="52.396061" x="-90.107155" y="203.17317" />
        <path style="fill:#000000;fill-rule:evenodd;stroke:#000000;stroke-width:1.05582392px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" d="m 148.7296,547.49026 -220.637008,0 0,0" id="path4288" inkscape:connector-curvature="0" />
        <g style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="flowRoot4302" />
        <ellipse style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1.47842932px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path3349-1-0" cx="-87.03698" cy="863.2276" rx="21.40518" ry="14.970306" />
        <rect style="fill:#f2f2f2;fill-opacity:1" id="rect4192-1-1-9-1" width="110.40601" height="60.835773" x="-976.95081" y="39.450497" transform="matrix(0,-1,1,0,0,0)" />
        <rect style="fill:#f2f2f2;fill-opacity:1;stroke:#000000;stroke-opacity:0.98224852" id="rect4192-1-1-9-6" width="110.40601" height="60.835773" x="-758.51715" y="-46.298756" transform="matrix(0,-1,1,0,0,0)" />
        <rect style="fill:#f2f2f2;fill-opacity:1;stroke:#000000;stroke-opacity:0.98224852" id="rect4192-1-1-9-6-0" width="110.40601" height="60.835773" x="-759.53809" y="53.483723" transform="matrix(0,-1,1,0,0,0)" />
    </g>
</svg>

通过清理和重建项目解决了这些错误。它们可能是由更改xml布局文件引起的,并且没有相关.java文件中的更改。