当前位置: 首页 > 知识库问答 >
问题:

Android-NullPointerException使用ViewGroup

东门奕
2023-03-14
 private RMProject currentProject;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.open_proj);
    Bundle bundle = getIntent().getExtras();
    currentProject = bundle.getParcelable("current");
    TextView textView = (TextView) findViewById(R.id.label_open_project);
    textView.setText("Project "+currentProject.getName());

}
public class EventsActivity extends Activity {
private RMProject currentProject;
private ListView listView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventDBManager.init(this);
    ViewGroup contentView = (ViewGroup) getLayoutInflater().inflate(R.layout.orm_event_list, null);
    listView = (ListView) contentView.findViewById(R.id.ormListView);
    Bundle bundle = getIntent().getExtras();
    currentProject = bundle.getParcelable("current");
    TextView textView = (TextView) findViewById(R.id.ormTextView);
    //38 line - next, which cause exception
    textView.setText("Project "+currentProject.getName());
    Button btn = (Button) contentView.findViewById(R.id.ormButtonAdd);
    setupButton(btn);
    setContentView(contentView);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="true"
        android:text="Project name"
        android:id="@+id/ormTextView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="90dp"
        android:textSize="20dp"/>
<Button
        android:id="@+id/ormButtonAdd"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add " />
<ListView
        android:id="@+id/ormListView"
        android:layout_above="@id/ormButtonAdd"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

和例外情况:

共有1个答案

丘智志
2023-03-14
TextView textView = (TextView) findViewById(R.id.ormTextView);

因此修复方法是将其替换为

TextView textView = (TextView) contentView .findViewById(R.id.ormTextView);

希望这有帮助

 类似资料:
  • 我的程序有问题。我使用一个数据库来保存我的应用程序的设置。当我在应用程序中保存密码时,一切正常,但当我用原则上相同的方法保存一个“学校班级”的名称时,另一个表崩溃了,我得到了这样的错误: 这是应该在数据库中写入的方法:

  • 我正在尝试从Android支持设计库中实现。我像这样初始化: 这很奇怪,因为我可以通过点击按钮或其他动作来改变状态。请帮帮我。

  • 我正在使用android的Sugar lib并检查了他们网站上的示例:hier 例外: androidManifest: 糖 lib 的.jar被下载并放在 libs 文件夹中。我不知道为什么它会引发这个异常。我刚刚遵循了这个网站的示例: hier

  • 问题内容: 我有两个按钮,它将播放声音以通知您正确的选择或错误的选择。这是我的方法: 同样,声音正确。在大多数情况下,它都可以正常工作,但是当我多次单击它时,随机出现此错误: 基本上说是行 playError.start(); 给我 NullPointerException (仅有时) 编辑: 行是148:playError.start(); 而falseAnswerPoints()是: 相同的是

  • 我运行以下命令(它是一个lib项目,但我也在常规项目中得到它):

  • 问题内容: 使用片段时,有时会出现以下异常: 当通过尝试从管理器中删除片段时,会通过调用of 发生异常。 我似乎无法弄清楚到底是什么原因造成的?我认为这与删除碎片时未正确清理碎片的堆栈有关。 问题答案: 回答我自己的问题: (最终)在您致电和 编辑:而且,就像两次圈出和ShinyuX在评论中指出的一样; 当调用or ,and 方法时,可能还 调用后,事务将在FragmentManager中排队。结