我有一个使用JSON适配器的ListView,但是当我试图在另一个listView上设置另一个适配器时,我的应用程序因未知原因崩溃。
我正在使用TabLayout和PageAdapter。
当我评论以下行时(MainActivity.java,下面的方法1):
insListView.setAdapter(mJSONAdapterIns);
应用程序有效。
MainActivity getUpdates(方法1) - WORKS - 在TabFragment1.java中调用:
// 4. Access the ListView
mainListView = (ListView) findViewById(R.id.main_listview);
// 10. Create a JSONAdapter for the ListView
mJSONAdapter = new JSONAdapter(MainActivity.this, getLayoutInflater());
// Set the ListView to use the ArrayAdapter
mainListView.setAdapter(mJSONAdapter);
MainActivity getInstagram() - 方法2 - 崩溃 - 在TabFragment2.java中调用:
insListView = (ListView) findViewById(R.id.instagram_listview);
// 5. Set this activity to react to list items being pressed
//mainListView.setOnItemClickListener(MainActivity.this);
// 10. Create a JSONAdapter for the ListView
mJSONAdapterIns = new JSONAdapterIns(MainActivity.this, getLayoutInflater());
// Set the ListView to use the ArrayAdapter
insListView.setAdapter(mJSONAdapterIns); //Crashes if this line is NOT commented
我使用MainActivity从Tab片段调用这两种方法。
TabFragment1.java - 效果很好:
public View onCreateView(最终的LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){
final View v = inflater.inflate(R.layout.fb_list, container, false);
MainActivity king = (MainActivity)getActivity();
king.getUpdates(); // Setting the first list adapter (Method 1)
return v;
}
getUpdates()正在更新我的第一个listview,一切都很好。
TabFragment2.java - 调用"崩溃"方法:
public class TabFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.instagram_list, container, false);
MainActivity king = (MainActivity)getActivity();
king.getInstagram(); //Causes crash in the setAdapter line
return v;
}
}
Logcat错误:
1647-1647/il.co.test.test W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2cfab20)
08-26 10:59:45.050 1647-1647/il.co.test.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: il.co.test.test, PID: 1647
java.lang.NullPointerException
at il.co.test.test.MainActivity.getInstagram(MainActivity.java:175)
at il.co.test.test.TabFragment2.onCreateView(TabFragment2.java:20)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:535)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
08-26 10:59:59.010 1647-1647/? I/Process﹕ Sending signal. PID: 1647 SIG: 9
我如何修复这次崩溃?
编辑1: 试图在方法2中添加以下行并注释setAdapter。
TextView test = (TextView) findViewById(R.id.x1);
test.setText("TEST");
应用程序也崩溃了。
看起来像findViewById返回NULL。为什么呢?
instagram_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:layoutDirection="rtl"
android:textDirection="rtl"
android:background="#ffffff">
<TextView
android:id="@+id/x1"
android:text="TEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- List whose dataset is defined in code with an adapter -->
<ListView
android:id="@+id/instagram_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layoutDirection="rtl"
android:textDirection="rtl"
android:divider="@android:color/transparent"
android:dividerHeight="20.0sp"
/>
</LinearLayout>
Prerak回答后的新logcat:
08-26 11:59:12.720 1599-1599/il.co.test.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: il.co.test.test, PID: 1599
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4532)
at il.co.test.test.MainActivity.getInstagram(MainActivity.java:172)
at il.co.test.test.TabFragment2.onCreateView(TabFragment2.java:25)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:535)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit
答案 0 :(得分:1)
直接从您的片段中获取ListView
。
private ListView insListView ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.instagram_list, container, false);
insListView = (ListView) v.findViewById(R.id.instagram_listview);
//MainActivity king = (MainActivity)getActivity();
//king.getInstagram(); //Causes crash in the setAdapter line
return v;
}
搜索如何将片段与活动进行通信,这可能对您有所帮助。
答案 1 :(得分:1)
在instagram_list.xml
这样的MainActivity.java
中夸大你的LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate(R.layout.instagram_list, null);
:
ListView
之后,实例化您的insListView = (ListView) view.findViewById(R.id.instagram_listview);
getInstagram()
因此LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate(R.layout.instagram_list, null);
insListView = (ListView) view.findViewById(R.id.instagram_listview);
mJSONAdapterIns = new JSONAdapterIns(MainActivity.this, getLayoutInflater());
insListView.setAdapter(mJSONAdapterIns);
方法的完整代码将是:
view
或@juanhl在评论中建议的其他方法可以将TabFragment2
从getInstagram()
传递到final View v = inflater.inflate(R.layout.instagram_list, container, false);
MainActivity king = (MainActivity)getActivity();
king.getInstagram(v); //Pass the view as an agruement
方法,如下所示:
getInstagram()
在您的public void getInstagram(View v)
{
insListView = (ListView) v.findViewById(R.id.instagram_listview);
mJSONAdapterIns = new JSONAdapterIns(MainActivity.this, getLayoutInflater());
insListView.setAdapter(mJSONAdapterIns);
}
方法中,您可以这样做:
$fr=fopen("php://stdin","r");
$input = fgets($fr,128);
$input = rtrim($input);
if(!is_numeric($input))
{
$in=str_split($input);
$s=sizeof($in);
$final='';
for($i=$s-1;$i>=0;$i--){
$final=$final.$input[$i];
}
echo $final;
}