0
我的應用程序甚至不會運行,我想知道我的代碼是什麼問題導致此錯誤發生?我正在嘗試使用數組填充ListView,但由於某種原因,應用程序甚至無法運行。致命的例外:(聽起來很糟糕)無法啓動活動ComponentInfo
FirstFragment.java
package layout;
import android.content.Context;
import android.net.Uri;
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.ArrayAdapter;
import android.widget.ListView;
import com.lucasapps.fakeproject.R;
import java.util.ArrayList;
public class FirstFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public FirstFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
public ArrayAdapter<String> mNewsTextAdapter;
public ArrayAdapter<Integer> mNewsImageAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first,container,false);
ArrayList<String> rightArray = new ArrayList<>();
rightArray.add("Trump Vs. Clinton");
rightArray.add("Twins Sign Clayton Kershaw");
rightArray.add("German Officials Identify Suspect");
rightArray.add("Silly Party Candidate John Johnson Seeks Debates");
ArrayList<Integer> leftArray = new ArrayList<>();
leftArray.add(R.drawable.picture_trumpvsclinton);
leftArray.add(R.drawable.picture_claytonkershaw);
leftArray.add(R.drawable.picture_germany);
leftArray.add(R.drawable.picture_johnjohnson);
mNewsTextAdapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_first,R.id.listView_text_news,rightArray);
//ListView leftListView = (ListView) rootView.findViewById(R.id.listView_text_newsImages);
ListView rightListView = (ListView) rootView.findViewById(R.id.listView_text_news);
//leftListView.setAdapter(mNewsImageAdapter);
rightListView.setAdapter(mNewsTextAdapter);
//ListView.setOnItemClickListeners
// leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
// }
// });
rightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
return inflater.inflate(R.layout.fragment_first, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
MainActivity.java
package com.lucasapps.fakeproject;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import layout.FirstFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null){
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FirstFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
錯誤
07-23 23:29:20.192 1162-1162/? I/art: Late-enabling -Xcheck:jni
07-23 23:29:20.192 1162-1162/? I/art: VMHOOK: rlim_cur : 0 pid:1162
07-23 23:29:20.242 1162-1174/? I/art: Debugger is no longer active
07-23 23:29:20.383 1162-1162/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lucasapps.fakeproject, PID: 1162
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lucasapps.fakeproject/com.lucasapps.fakeproject.MainActivity}: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener
at layout.FirstFragment.onAttach(FirstFragment.java:100)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1288)
at android.app.Activity.performStart(Activity.java:5974)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
07-23 23:29:24.167 1162-1162/com.lucasapps.fakeproject D/Process: killProcess, pid=1162
07-23 23:29:24.167 1162-1162/com.lucasapps.fakeproject D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
添加崩潰報告。 –
查看logcat中的堆棧跟蹤 –