2016-03-28 18 views
-5

必要な出力は、第1アクティビティのタイトル、画像ビュー、および3つのボタンです。画像がクリックされると、新しい内容が表示されますが、今回はその投稿のみが表示されます(後でコメントとともに表示されます)。だから私はバンドルを使用してタイトルと画像の詳細を渡す必要がありますが、私のアプリケーションがクラッシュします。nullオブジェクト参照で仮想メソッド 'void android.widget.TextView.setText(java.lang.CharSequence)'を呼び出そうとしました - putExtraの束

CustomAdapter.javaコード:

package com.example.swapsha96.myapplication; 

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 

class CustomAdapter extends ArrayAdapter<String> { 

private final String[] itemname; 
private final Integer[] imgid; 
private final Context context; 

public CustomAdapter(Context context, String[] resource, Integer[] imgid) { 
    super(context, R.layout.postcard, resource); 
    this.itemname=resource; 
    this.imgid=imgid; 
    this.context = context; 
} 

@Override 
public View getView(final int position, View convertView, final ViewGroup parent) { 
    LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
    View view = layoutInflater.inflate(R.layout.postcard, parent, false); 

    TextView title = (TextView)view.findViewById(R.id.title); 
    ImageView post = (ImageView)view.findViewById(R.id.post); 
    Button plus1 = (Button)view.findViewById(R.id.plus1); 
    Button share = (Button)view.findViewById(R.id.share); 
    Button comment = (Button)view.findViewById(R.id.comment); 

    title.setText(itemname[position]); 
    post.setImageResource(imgid[position]); 
    post.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(parent.getContext(), Post.class); 
      Bundle extras = new Bundle(); 
      extras.putString("title",itemname[position]); 
      extras.putInt("post", imgid[position]); 
      intent.putExtras(extras); 
      parent.getContext().startActivity(intent); 
     } 
    }); 

    return view; 
} 
} 

Post.javaコード:

package com.example.swapsha96.myapplication; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Post extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_post); 

    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    String titleValue = extras.getString("title"); 
    Integer postValue = extras.getInt("post"); 

    TextView title = (TextView)findViewById(R.id.title); 
    ImageView post = (ImageView)findViewById(R.id.post); 
    title.setText(titleValue); 
    post.setImageResource(postValue); 
} 

} 

activity_post.xmlポスト(画像クリックで次のオープンしようとしているアクティビティクラス):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:id="@+id/postcard" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context="com.example.swapsha96.myapplication.Post" 
    android:background="#FFFFFF" 
    android:layout_margin="2dp" 
    android:adjustViewBounds="true"> 

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/myCoordinator" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/myScrollingContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     android:fillViewport="true"> 

     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listView" 
      android:layout_gravity="center_horizontal" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="2dp" /> 

    </android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout> 

このエラーは、title.setText(titleValue);というエラーをスローします。 2番目のコードでは、オブジェクト参照がnullであるため、アクティビティを開始できません。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swapsha96.myapplication/com.example.swapsha96.myapplication.Post}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
+0

あなたも 'activity_post' XMLを追加することができますか? –

+1

この行は問題です - > 'TextView title =(TextView)findViewById(R.id.title);' nullを返すためです。 XMLお願いしますか? – Vucko

+0

私は間違った 'R.id.title'の問題だと思っています –

答えて

0

あなたがlayoutのid title持つ任意のTextViewを宣言していません。だから、textviewが見つからず、したがってtitleオブジェクトがnullです。

は、このようなあなたのレイアウトにTextViewを追加します。

<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" "/> 
関連する問題