我一直在將代碼從活動轉移到片段,我有simillar應用程序,完美的作品,我檢查了每個感染,看起來像所有匹配,但面臨這個問題。ListView setAdapter null對象引用
實際的錯誤:
Process: com.example.android.miwok, PID: 7432 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.Numbers_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.android.miwok.numeriuFragmentai.onCreateView(numeriuFragmentai.java:81) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:602) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256) at android.app.Activity.performStart(Activity.java:6959) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
與其他呈三角代碼都匹配檢查,但我敢肯定,我錯過了什麼。
numeriuFragmentai.java
package com.example.android.miwok;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.android.miwok.word;
import com.example.android.miwok.Adaptoreditor;
import android.view.LayoutInflater;
import java.util.ArrayList;
import static com.example.android.miwok.R.id.container;
public class numeriuFragmentai extends Fragment {
private MediaPlayer mMediaPlayer;
private AudioManager mAudio;
private AudioManager.OnAudioFocusChangeListener focuslistener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
{
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
}
else if (focusChange==AudioManager.AUDIOFOCUS_GAIN)
{
mMediaPlayer.start();
}
else if (focusChange==AudioManager.AUDIOFOCUS_LOSS)
{
releaseMediaPlayer();
}
}
};
private MediaPlayer.OnCompletionListener completed = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releaseMediaPlayer();
}
};
public numeriuFragmentai() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_item, container, false);
mAudio=(AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
final ArrayList<word> numbers = new ArrayList<word>();
numbers.add(new word(R.string.number_one, R.string.miwok_number_one,R.drawable.yplay, R.raw.number_one, R.drawable.number_one));
numbers.add(new word(R.string.number_two, R.string.miwok_number_two,R.drawable.yplay, R.raw.number_two, R.drawable.number_two));
numbers.add(new word(R.string.number_three, R.string.miwok_number_three,R.drawable.yplay, R.raw.number_three, R.drawable.number_three));
numbers.add(new word(R.string.number_four, R.string.miwok_number_four,R.drawable.yplay, R.raw.number_four, R.drawable.number_four));
numbers.add(new word(R.string.number_five, R.string.miwok_number_five, R.drawable.yplay, R.raw.number_five, R.drawable.number_five));
numbers.add(new word(R.string.number_six, R.string.miwok_number_six,R.drawable.yplay, R.raw.number_six, R.drawable.number_six));
numbers.add(new word(R.string.number_seven, R.string.miwok_number_seven,R.drawable.yplay, R.raw.number_seven, R.drawable.number_seven));
numbers.add(new word(R.string.number_eight, R.string.miwok_number_eight,R.drawable.yplay, R.raw.number_eight, R.drawable.number_eight));
numbers.add(new word(R.string.number_nine, R.string.miwok_number_nine,R.drawable.yplay, R.raw.number_nine, R.drawable.number_nine));
numbers.add(new word(R.string.number_ten, R.string.miwok_number_ten,R.drawable.yplay, R.raw.number_ten, R.drawable.number_ten));
Adaptoreditor adapteris = new Adaptoreditor(getActivity(), numbers, R.color.numeriai);
Adaptoreditor adapter = new Adaptoreditor(getActivity(), numbers, R.color.spalvos);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
releaseMediaPlayer();
word words = numbers.get(position);
int result = mAudio.requestAudioFocus(focuslistener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mMediaPlayer = MediaPlayer.create(getActivity(), words.getSound());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(completed);
}
}
});
return rootView;
}
@Override
public void onStop() {
super.onStop();
releaseMediaPlayer();
}
private void releaseMediaPlayer()
{
if (mMediaPlayer!=null)
{
mMediaPlayer.release();
mMediaPlayer=null;
mAudio.abandonAudioFocus(focuslistener);
}
}
}
NumbersActivity.java
package com.example.android.miwok;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Numbers_activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new numeriuFragmentai())
.commit();
}
}
Adaptoreditor.java
package com.example.android.miwok;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.android.miwok.word;
import java.util.ArrayList;
public class Adaptoreditor extends ArrayAdapter<word> {
private int mcolorid;
public Adaptoreditor(Context context, ArrayList<word> word, int colorid) {
super(context, 0, word);
mcolorid=colorid;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
word vertimas = getItem(position);
TextView nameTextView = (TextView) listItemView.findViewById(R.id.first);
nameTextView.setText(vertimas.getMiwok());
TextView numberTextView = (TextView) listItemView.findViewById(R.id.second);
numberTextView.setText(vertimas.getTranslated());
ImageView img = (ImageView) listItemView.findViewById(R.id.img);
if (vertimas.yrft()) {
img.setImageResource(vertimas.getImgR());
img.setVisibility(View.VISIBLE);
} else {
img.setVisibility(View.GONE);
}
ImageView pic = (ImageView)listItemView.findViewById(R.id.player);
pic.setImageResource(vertimas.getPlayImg());
View vieta = listItemView.findViewById(R.id.linearas);
int color = ContextCompat.getColor(getContext(), mcolorid);
vieta.setBackgroundColor(color);
return listItemView;
}
}
word.java
package com.example.android.miwok;
public class word {
private int mtranslated;
private int mmiwok;
private int mimgid=NER_FOTO;
private int mpImg;
private int mSound;
public final static int NER_FOTO = -1;
public word (int translated, int miwok, int pimg,int sound)
{
mtranslated = translated;
mmiwok=miwok;
mpImg=pimg;
mSound=sound;
}
public word (int translated, int miwok,int pimg, int sound,int imgid)
{
mtranslated = translated;
mmiwok=miwok;
mimgid=imgid;
mpImg=pimg;
mSound=sound;
}
public int getMiwok()
{
return mmiwok;
}
public int getTranslated()
{
return mtranslated;
}
public int getSound(){return mSound;}
public int getImgR()
{
return mimgid;
}
public int getPlayImg()
{
return mpImg;
}
public boolean yrft()
{
return mimgid !=NER_FOTO;
}
}
activity_category.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
List_item.XML
<?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="88dp"
android:id="@+id/linearas"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="32dp"
android:layout_height="40dp"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:id="@+id/player"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="88dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="88dp"
android:layout_height="88dp"
tools:src="@mipmap/ic_launcher"
android:id="@+id/img"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
tools:text="Pirmas" />
<TextView
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Antras" />
</LinearLayout>
</LinearLayout>
</RelativeLayout
Wordlist.XML
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"/>
鏈接到整個應用程序 App
這有幫助,謝謝! –