2014-06-09 71 views
-1
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; 
     } 
    } 

} 

獲得空指針異常的線路:43 我評論Line43 這是我的Android項目獲得的部分動態線性佈局 請幫我找出錯誤,我的完整logcat是,謝謝。無法啓動活動ComponentInfo空指針異常

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

安置自己的''activity_search_result.xml –

+0

我認爲你需要從片段級訪問您的佈局,如果您發佈'activity_search_result'我們可以看到, –

+0

你必須做你的工作,在片段類作爲你不沒有linarLayout我n'activity_search_result' –

回答

0

哪裏是你的LinearLayout T1,裏面activity_search_result.xml或fragment_search_result?這可能是錯誤的...

+0

Zoran這個必須作爲評論,但正如我們可以看到佈局文件,你可以改善你的答案。 –

1

在你onCreate()

setContentView(R.layout.activity_search_result); 

所以線性佈局「TR」在activity_search_result引用,這就是爲什麼TextView的引用時的NPE。

發生這種情況的活動,其中作爲linearlayout片段佈局內大概定義,(請留言fragment_search_result.xml,將相應地刪除或更新應答)

所以你應該findViewByIdonCreateView像移:

@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; 
     } 
+0

謝謝但它沒有顯示任何文字,它應該顯示「TYPE:」右邊 – brk8055

+0

背景的顏色是什麼?你有沒有使用任何權重或哪些是XML中的屬性? – user2450263

+0

它的白色,我已經將tr.setTextColor更改爲DKGRAY,但它不顯示。 – brk8055

相關問題