0

我试图以编程方式将文本视图添加到线性布局,但我的线性布局上一直有一个空指针。它在布局膨胀后被调用,所以我不明白它为什么会发生。这是代码。

tile_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/master"
android:orientation="horizontal"    
android:layout_width="fill_parent"   
android:layout_height="match_parent"> 
<TextView
android:id="@+id/fragment_two_directory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Testing Fragment Two" />



</LinearLayout>

fragment_two.java

public class fragment_two extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.tile_layout,container, false);
}

public static fragment_two newInstance(String directory, Activity activity) {
    fragment_two ft = new fragment_two();
    TextView text = (TextView)activity.findViewById(R.id.fragment_two_directory);
    text.setText(directory);
    return ft;
}
public static void setDirectoryText(String directory, Activity activity) {
    TextView directoryText = (TextView)activity.findViewById(R.id.fragment_two_directory);
    directoryText.setText(directory);
    populateDirectory(directory, activity);
}
public static void populateDirectory(String directory, Activity activity) {
    LinearLayout masterLinear = (LinearLayout)activity.findViewById(R.id.master);
    TextView testText = new TextView(activity);
    testText.setText("test");
    masterLinear.addView(testText);
    //testText.setText("Testing Add View");
    //for (int x=0; x < 5; x++) {
        //FrameLayout container = new FrameLayout(getActivity());
        //container.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //ImageView fileImage = new ImageView(getActivity());
        //fileImage.setLayoutParams(new FrameLayout.LayoutParams(110, 110));
        //fileImage.setImageResource(R.drawable.blank_file);
        //container.addView(fileImage);
        //TextView fileName = new TextView(getActivity());
        //fileName.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //fileName.setText("Filename.txt");
        //container.addView(fileName);
        //masterLinear.addView(container);
    //}

}
}

最后是主要活动

package com.bv.dual_fragments;

import com.bv.dual_fragments.fragment_one.OnArticleSelectedListener;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

    public class dual_fragments extends Activity implements OnArticleSelectedListener {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        //((global_holder)this.getApplication()).setDirectory("test"); 

    }
    @Override
    public void onArticleSelected(String directory) {
        //fragment_two frag_two = (fragment_two)getFragmentManager().findFragmentById(R.id.container);
        //TextView containerText = (TextView)findViewById(R.id.fragment_two_directory);
        //containerText.setText(directory);
        fragment_two.populateDirectory(directory, this);
    }
}

这是 LogCat

03-06 17:35:47.452: ERROR/AndroidRuntime(3576): FATAL EXCEPTION: main
03-06 17:35:47.452: ERROR/AndroidRuntime(3576): java.lang.NullPointerException
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at     com.bv.dual_fragments.fragment_two.populateDirectory(fragment_two.java:36)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.bv.dual_fragments.dual_fragments.onArticleSelected(dual_fragments.java:25)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.bv.dual_fragments.fragment_one.onListItemClick(fragment_one.java:88)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AdapterView.performItemClick(AdapterView.java:282)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AbsListView.performItemClick(AbsListView.java:1032)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2447)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3077)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.ListView.onTouchEvent(ListView.java:3566)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.View.dispatchTouchEvent(View.java:4600)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1488)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1256)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1700)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1270)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.app.Activity.dispatchTouchEvent(Activity.java:2271)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1680)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2272)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1958)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.os.Looper.loop(Looper.java:126)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.app.ActivityThread.main(ActivityThread.java:3997)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at java.lang.reflect.Method.invoke(Method.java:491)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at dalvik.system.NativeStart.main(Native Method)

我还在蜂窝模拟器中运行了它,并查看了层次结构查看器。我在该布局文件中有两个线性布局,它们都没有出现,只有文本视图。

4

3 回答 3

1

好的,问题是布局文件搞砸了,我有两个线性布局水平方向,因此层次结构不会加载它们,给出空点异常。对于任何查看 Android 3.0 的 heirarchyviewer 的人来说,这只是一个提示,片段(在布局文件中)将自动获得其膨胀视图中的第一个布局标签的所有权。所以说你有

<fragment android:id="@+id/viewer" />

在你的布局文件中你有

<LinearLayout android:id="@+id/first_layout" />

在层次结构查看器中,LinearLayout 将显示为查看器,而不是 first_layout,因为片段没有默认布局,它会将其布局文件中的第一个布局容器重新标识为自己的 id。

于 2011-03-07T03:33:15.560 回答
0

R.id.tile_layout如果您要查找,是否应该将内容视图设置为R.id.master

现在你正在寻找它test.xml

于 2011-03-07T02:23:08.310 回答
0

您确定在 OnCreate() 之后调用 OnArticleSelected() 吗?如果 findViewById(id) 在调用 setContentView(layout) 之前被调用,它将返回 null。也尝试改变这一行:

fragment_two.populateDirectory(directory, this);

对此:

fragment_two.populateDirectory(directory, dual_fragments.this);

如果由于某种原因传递了活动以外的其他内容,它将破坏事物。我知道在从 OnClickListener 内部为按钮进行调用时,您需要像这样与众不同,但我不熟悉您正在使用的侦听器,所以我不确定是否有必要。Matthew 提出了一个很好的观点,即确保您指向正确的 .xml 文件。

于 2011-03-07T02:31:31.550 回答