2017-08-03 27 views
0

我送在MainActivity適配器數據到另一個打算,然後在TextView中顯示一個null對象的虛方法無效,但它給了我發出TextView中不能顯示Intent.getStringExtra並返回試圖調用

主要Activity.java

Article m = articleList.get(position); 
int catidd = (int) view.getTag(); 
intent.putExtra(cat_id, String.valueOf(catidd)); 
intent.putExtra("title", m.title); 
intent.putExtra("thumbnailUrl", m.thumbnailUrl); 
intent.putExtra("description", m.description); 
startActivity(intent); 

這將參數發送細節意圖...... Detailview.java

TextView description = (TextView) findViewById(R.id.detail_description); 

final String catid = intent.getStringExtra(MainActivity.cat_id); 
// String message = "Test"; 
id = catid; 
//String title = intent.getExtras().getString("title"); 
pDialog = new ProgressDialog(this); 
// Showing progress dialog before making http request 
pDialog.setMessage(this.getIntent().getStringExtra("description")); 
pDialog.show(); 

description.setText(this.getIntent().getStringExtra("description")); 

現在對話框顯示數據但description.setText給出空的異常返回試圖調用一個空對象

content_detailview.xml虛擬方法空隙>佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="3.2" 
    > 

    <RelativeLayout 
     android:id="@+id/viewA" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.6" 
     android:background="@drawable/articleimg" 
     android:gravity="center_horizontal|center_vertical"></RelativeLayout> 

    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="3.24" 
     android:orientation="vertical" 
     android:padding="10dp" 
     android:weightSum="10" 
     > 



     <LinearLayout 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:weightSum="4" 
      android:orientation="horizontal" 
      > 
      <TextView 
       android:id="@+id/detail_description" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:text="Name" 
       android:textSize="22dp" 
       android:textColor="@android:color/black" 
       android:padding="3dp" 
       /> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

<TextView 
    android:id="@+id/detail_title" 
    android:layout_width="300dp" 
    android:layout_height="300dp" 
    android:layout_margin="6dp" 
    android:clickable="false" 
    android:elevation="10dp" 
    android:textSize="40dp" 
    android:gravity="top|left" 
    android:textColor="@android:color/white" 
    app:layout_anchor="@id/viewA" 
    app:layout_anchorGravity="bottom"/> 


<TextView 
    android:id="@+id/floating" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_margin="10dp" 
    android:gravity="top" 
    android:clickable="false" 
    android:background="@drawable/dots" 
    app:layout_anchor="@id/viewA" 
    app:layout_anchorGravity="bottom|end" 
    /> 

+0

'getIntent()getStringExtra(MainActivity.cat_id);' –

回答

0

我findViewById之後調用的setContentView()( )這是錯誤的。

直到setContentView()發生,視圖不到位,因此它不能被詢問它的內容。將最後一行向上移一點,它應該可以解決問題。

更正

setContentView(R.layout.activity_show_second); 
textView = (TextView)findViewById(R.id.textViewdata); 
textView.setText(message);