无法启动活动ComponentInfo Null Pointer Exception

时间:2014-06-09 10:56:36

标签: java android android-intent

package com.example.nrbapp;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
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.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.os.Build;

public class SearchResult extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            int check = extras.getInt("check");
            ArrayList<String> K_array = extras.getStringArrayList("K_array");
        }   
        setContentView(R.layout.activity_search_result);
        LinearLayout t1 = (LinearLayout) findViewById(R.id.linear_layout);
        TextView tr = new TextView(this);
        //tr.setId(95);// define id that must be unique
        tr.setText("TYPE:"); // set the text for the header 
        tr.setTextColor(Color.WHITE); // set the color
        tr.setTypeface(null, Typeface.BOLD);
        tr.setPadding(5, 5, 5, 5); // set the padding (if required)
        t1.addView(tr);//**LINE 43**
        //t1.addView(tr, new LinearLayout.LayoutParams(
          //      LayoutParams.FILL_PARENT,
            //    LayoutParams.WRAP_CONTENT));

        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.search_result, 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_search_result,
                    container, false);
            return rootView;
        }
    }

}
  

获取Null Pointer Exception行:43   我评论过Line43   这是我的android项目的一部分,以获得动态线性布局   请帮我找出错误,我的完整logcat是,谢谢。

06-09 06:50:03.590: E/AndroidRuntime(3794): FATAL EXCEPTION: main
06-09 06:50:03.590: E/AndroidRuntime(3794): Process: com.example.nrbapp, PID: 3794
06-09 06:50:03.590: E/AndroidRuntime(3794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nrbapp/com.example.nrbapp.SearchResult}: java.lang.NullPointerException
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.os.Looper.loop(Looper.java:136)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at java.lang.reflect.Method.invoke(Method.java:515)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at dalvik.system.NativeStart.main(Native Method)
06-09 06:50:03.590: E/AndroidRuntime(3794): Caused by: java.lang.NullPointerException
06-09 06:50:03.590: E/AndroidRuntime(3794):     at com.example.nrbapp.SearchResult.onCreate(SearchResult.java:43)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.Activity.performCreate(Activity.java:5231)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-09 06:50:03.590: E/AndroidRuntime(3794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-09 06:50:03.590: E/AndroidRuntime(3794):     ... 11 more
06-09 06:50:04.950: I/Process(3794): Sending signal. PID: 3794 SIG: 9
06-09 06:50:05.820: D/AndroidRuntime(3811): Shutting down VM
06-09 06:50:05.820: W/dalvikvm(3811): threadid=1: thread exiting with uncaught exception (group=0xb3a9eba8)
06-09 06:50:05.830: E/AndroidRuntime(3811): FATAL EXCEPTION: main
06-09 06:50:05.830: E/AndroidRuntime(3811): Process: com.example.nrbapp, PID: 3811
06-09 06:50:05.830: E/AndroidRuntime(3811): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nrbapp/com.example.nrbapp.SearchResult}: java.lang.NullPointerException
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.os.Looper.loop(Looper.java:136)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at java.lang.reflect.Method.invoke(Method.java:515)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at dalvik.system.NativeStart.main(Native Method)
06-09 06:50:05.830: E/AndroidRuntime(3811): Caused by: java.lang.NullPointerException
06-09 06:50:05.830: E/AndroidRuntime(3811):     at com.example.nrbapp.SearchResult.onCreate(SearchResult.java:43)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.Activity.performCreate(Activity.java:5231)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-09 06:50:05.830: E/AndroidRuntime(3811):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-09 06:50:05.830: E/AndroidRuntime(3811):     ... 11 more
  

这是我的activity_search_result.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="com.example.nrbapp.SearchResult"
tools:ignore="MergeRootFrame">

<LinearLayout
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="39dp"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/textView100"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textIsSelectable="true"
        android:textSize="12sp"
        android:textStyle="bold"
        />
</LinearLayout>
</FrameLayout>

2 个答案:

答案 0 :(得分:1)

onCreate()

setContentView(R.layout.activity_search_result);  

因此在activity_search_result中引用了线性布局“tr”,这就是为textview引用NPE的原因。

对于活动会发生这种情况,因为您的linearlayout可能是在片段布局中定义的(请发布fragment_search_result.xml,相应地删除或更新答案)

所以你应该将findViewById改为onCreateView,如:

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_search_result,
                    container, false);
    //now declare linearlayout to be found in fragment_search_result.xml
    LinearLayout t1 = (LinearLayout) rootView.findViewById(R.id.linear_layout);
        TextView tr = new TextView(SearchResult.this);
        //tr.setId(95);// define id that must be unique
        tr.setText("TYPE:"); // set the text for the header 
        tr.setTextColor(Color.WHITE); // set the color
        tr.setTypeface(null, Typeface.BOLD);
        tr.setPadding(5, 5, 5, 5); // set the padding (if required)
        t1.addView(tr);
            return rootView;
        }

答案 1 :(得分:0)

您的LinearLayout t1在哪里,在activity_search_result.xml或fragment_search_result中?那可能是错误的......