1
私は、答えられる質問のリストがあるトレーニングプログラムを実施しています。回答テキストに応じて、質問の色が変わります。結果は別のアクティビティで表示する必要があります。バイナリXMLファイルライン#22:クラスフラグメントを拡張するエラー
MainActivity:
エラー膨張クラスフラグメントここコードです:私は、テキストの問題の色ですが、私はそれらのエラーバイナリXMLファイルのライン#22が持っているもの救うために断片を使用することを示唆しました
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// public static String TAG = "MYLOG_main_activity";
public static String KEY = "KEY";
RecyclerView mRecyclerView;
LinearLayoutManager mLinearLayoutManager;
Context mContext;
Button mResult;
public static int[] iResult;
public static void MyReceiver(int[] result)
{
iResult = result;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
iResult = savedInstanceState.getIntArray(KEY);
}
mResult = (Button) findViewById(R.id.result);
mResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View asv) {if(iResult != null) {
/* Intent intent1 = new Intent("Intent");
intent1.putExtra("message", iResult);
intent1.setAction("broadcast");
sendBroadcast(intent1);
*/
Intent intent = new Intent(MainActivity.this, Result.class);
startActivity(intent);
Result.receiver(iResult);
}
}
});
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLinearLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
ArrayList<Ask> asks = new ArrayList<>();
asks.add(0,new Ask(0,R.drawable.image1,R.string.one));
asks.add(1,new Ask(1,R.drawable.image2,R.string.two));
asks.add(2,new Ask(2,R.drawable.image3,R.string.three));
asks.add(3,new Ask(3,R.drawable.image4,R.string.four));
asks.add(4,new Ask(4,R.drawable.image5,R.string.five));
//iResult = new int[asks.size()];
RVAdapter adapter = new RVAdapter(asks);
mRecyclerView.setAdapter(adapter);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray(KEY, iResult);
}
}
RVAdapter:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 1 on 2.09.2016.
*/
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.CardViewHolder>{
public static final String TAG = "MYLOG_RVAdapter";
List<Ask> asks;
int[] iResult;
RVAdapter(List<Ask> asks) {
this.asks = asks;
iResult = new int[asks.size()];
for (int i = 0; i < asks.size(); i++) {
iResult[i] = 0;
}
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); //На эту строку ругается
Log.d(TAG,"onCreateViewHolder");
return new CardViewHolder(v);
}
@Override
public int getItemCount() {
return asks.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d(TAG,"onAttachedToRecyclerView");
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onBindViewHolder(final CardViewHolder holder, final int position) {
holder.mButtonNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.mTextView.setBackgroundResource(R.color.No);
iResult[position] = 2;
MainActivity.MyReceiver(iResult);
}
});
holder.mButtonYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.mTextView.setBackgroundResource(R.color.Yes);
iResult[position] = 1;
MainActivity.MyReceiver(iResult);
}
});
switch (position) {
case 0:
holder.mTextView.setText(R.string.one);
holder.mImageView.setImageResource(R.drawable.image1);
break;
case 1:
holder.mTextView.setText(R.string.two);
holder.mImageView.setImageResource(R.drawable.image2);
break;
case 2:
holder.mTextView.setText(R.string.three);
holder.mImageView.setImageResource(R.drawable.image3);
break;
case 3:
holder.mTextView.setText(R.string.four);
holder.mImageView.setImageResource(R.drawable.image4);
break;
case 4:
holder.mTextView.setText(R.string.five);
holder.mImageView.setImageResource(R.drawable.image5);
break;
}
Log.d(TAG,"onBindViewHolder");
Log.d(TAG, String.valueOf(position));
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
ImageView mImageView;
TextView mTextView;
CardView mCardVew;
Button mButtonYes;
Button mButtonNo;
public CardViewHolder(View itemView) {
super(itemView);
mButtonNo = (Button) itemView.findViewById(R.id.No);
mButtonYes = (Button) itemView.findViewById(R.id.Yes);
mTextView = (TextView) itemView.findViewById(R.id.textView);
mImageView = (ImageView) itemView.findViewById(R.id.imageView);
mCardVew = (CardView) itemView.findViewById(R.id.cardView);
Log.d(TAG,"CardViewHolder");
}
}
}
TextFragment:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 1 on 04.10.2016.
*/
public class TextFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView =
inflater.inflate(R.layout.fragment1, container,false);
return rootView;
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.vkramarenko.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:id="@+id/result"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/Yes"
android:layout_alignParentEnd="true" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:layout_above="@+id/result">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
項目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardView"
card_view:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no"
android:id="@+id/No"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.vkramarenko.myapplication.TextFragment"
android:id="@+id/textFragment"
android:layout_above="@+id/No"
android:layout_alignEnd="@+id/No" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes"
android:id="@+id/Yes"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/No" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/image1"
android:contentDescription=""
android:layout_above="@+id/textFragment"
android:layout_alignEnd="@+id/No" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
fragment1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/one"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
あなたのスタックトレースを投稿 – tynn