Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 由二进制XML文件导致的扩展布局错误_Java_Android_Android Layout_Nullpointerexception_Android Widget - Fatal编程技术网

Java 由二进制XML文件导致的扩展布局错误

Java 由二进制XML文件导致的扩展布局错误,java,android,android-layout,nullpointerexception,android-widget,Java,Android,Android Layout,Nullpointerexception,Android Widget,因此,我正在编写一个android应用程序,在单击通知后启动一项活动。我在日志中得到了这个: ` ` 我已经搜索了StackOverflow的答案,因为似乎其他人也有类似的问题-我尝试重建和清理项目,重新启动ADT以及创建一个新文件。我想知道这里是否有人有指导 这是我的布局文件: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/

因此,我正在编写一个android应用程序,在单击通知后启动一项活动。我在日志中得到了这个:

`

`

我已经搜索了StackOverflow的答案,因为似乎其他人也有类似的问题-我尝试重建和清理项目,重新启动ADT以及创建一个新文件。我想知道这里是否有人有指导

这是我的布局文件:

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view_sms"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="4dp"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    style="?android:listSeparatorTextViewStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/food_title_label" />

                <EditText
                    android:id="@+id/food_title_sms"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="16dp" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Units of Measurement:" />

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <CheckBox
                android:id="@+id/imperial_checkBox_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Imperial" />

            <CheckBox
                android:id="@+id/metric_checkBox_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Metric" />
        </TableRow>

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Number of Servings:" />


        <EditText
            android:id="@+id/food_servings_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" >
        </EditText>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/ingredient_text_sms"
                style="?android:listSeparatorTextViewStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ingredients" />
        </TableRow>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray" />

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Spinner
                    android:id="@+id/imperial_spinner_sms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:entries="@array/imperial_array"
                    android:visibility="gone" />

                <Spinner
                    android:id="@+id/metric_spinner_sms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:entries="@array/metric_array"
                    android:visibility="gone" />
            </RelativeLayout>

            <EditText
                android:id="@+id/amount_text_sms"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />

            <EditText
                android:id="@+id/ing_name_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:ems="10" />
        </TableRow>

        <!-- second ingredient -->



        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Directions" />

        <EditText
            android:id="@+id/directions_text_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine" />

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Options" />

        <Button
            android:id="@+id/save_button_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Save Recipe" />

            <Button
            android:id="@+id/dismiss_button_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Dismiss" />




        <!-- DONE -->

    </LinearLayout>

</ScrollView> 
奇怪的是,一个名为ActionBarOverlayLayout的小部件上有一个例外,它不在我的布局中。如果有人知道这会有很大帮助!(项目周一到期!)
克劳迪娅

为什么要覆盖onCreateView?在onCreate()中,使用布局资源设置内容。您的其他初始化代码应该放在此处或onResume()上,具体取决于它所执行的操作。试试这个

公共类SmsViewActivity扩展活动{
私有静态最终字符串TAG=“SmsViewActivity”;
私有FoodJSONSerializer mSerializer;
公共按钮mSaveButton、mDismissButton;
公共int通知ID;
公共字符串消息;
私有编辑文本mTitleField;
私有复选框mImperialCheckBox;
私有复选框;
私有编辑文本mServingsField;
私有EditText mDirectionsField;
私人纺纱机;
私人纺纱机;
食品

创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.view\u sms); mSaveButton=(按钮)findviewbyd(R.id.save\u Button\u sms); mDismissButton=(按钮)findViewById(R.id.disease\u Button\u sms); // ------------------------------------------------------------ //获取额外信息并在视图中显示信息 //字符串发送方=getIntent().getStringExtra(“发送方”); 字符串msg=getIntent().getStringExtra(“消息”); 试一试{ JSONObject jsonRecipe=新JSONObject(msg); 食品M食品=新食品(jsonRecipe); Log.i(标签“Food=“+mFood”); }捕获(JSONException e){ //TODO自动生成的捕捉块 e、 printStackTrace(); } // ----------------------------------------------------------------------- mNotificationId=getIntent().getIntExtra(“notificationid”,0); 如果(mNotificationId==0){ Log.e(标记“无法检索通知ID”); Toast.makeText(这是“SMS查看器中发生了致命错误”, Toast.LENGTH_LONG).show(); 完成(); } //取消通知 字符串ns=Context.NOTIFICATION\u服务; NotificationManager notificationMgr=(NotificationManager)getSystemService(ns); 通知经理取消(mNotificationId); // -------------------------------------------------- mTitleField=(编辑文本)诉findViewById(R.id.food\u title\u sms); mTitleField.setText(mFood.getTitle()); mServingsField=(编辑文本)诉findViewById(R.id.食品、服务和短信); mServingsField.setText(mFood.getServings()); mDirectionsField=(EditText)v.findViewById(R.id.directions\u text\u sms); mDirectionsField.setText(mFood.getDirections()); // -------------------------------------------------- //保存按钮的侦听器单击 setOnClickListener(新的OnClickListener(){ 公共void onClick(视图v){ 完成(); } }); //关闭按钮单击的侦听器 mDismissButton.setOnClickListener(新的OnClickListener(){ 公共void onClick(视图v){ 回溯(); 完成(); } }); 返回v; } 公共无效回溯列表(){ Intent i=新Intent(这个,FoodListFragment.class); 星触觉(i); } }
为什么要覆盖onCreateView?在onCreate()中,使用布局资源设置内容。您的其他初始化代码应该放在此处或onResume()上,具体取决于它所执行的操作。试试这个

公共类SmsViewActivity扩展活动{
私有静态最终字符串TAG=“SmsViewActivity”;
私有FoodJSONSerializer mSerializer;
公共按钮mSaveButton、mDismissButton;
公共int通知ID;
公共字符串消息;
私有编辑文本mTitleField;
私有复选框mImperialCheckBox;
私有复选框;
私有编辑文本mServingsField;
私有EditText mDirectionsField;
私人纺纱机;
私人纺纱机;
食品

创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.view\u sms); mSaveButton=(按钮)findviewbyd(R.id.save\u Button\u sms); mDismissButton=(按钮)findViewById(R.id.disease\u Button\u sms); // ------------------------------------------------------------ //获取额外信息并在视图中显示信息 //字符串发送方=getIntent().getStringExtra(“发送方”); 字符串msg=getIntent().getStringExtra(“消息”); 试一试{ JSONObject jsonRecipe=新JSONObject(msg); 食品M食品=新食品(jsonRecipe); Log.i(标签“Food=“+mFood”); }捕获(JSONException e){ //TODO自动生成的捕捉块 e、 printStackTrace(); } // ----------------------------------------------------------------------- mNotificationId=getIntent().getIntExtra(“notificationid”,0); 如果(mNotificationId==0){ 日志
   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view_sms"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="4dp"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    style="?android:listSeparatorTextViewStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/food_title_label" />

                <EditText
                    android:id="@+id/food_title_sms"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="16dp" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Units of Measurement:" />

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <CheckBox
                android:id="@+id/imperial_checkBox_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Imperial" />

            <CheckBox
                android:id="@+id/metric_checkBox_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Metric" />
        </TableRow>

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Number of Servings:" />


        <EditText
            android:id="@+id/food_servings_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" >
        </EditText>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/ingredient_text_sms"
                style="?android:listSeparatorTextViewStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ingredients" />
        </TableRow>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray" />

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Spinner
                    android:id="@+id/imperial_spinner_sms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:entries="@array/imperial_array"
                    android:visibility="gone" />

                <Spinner
                    android:id="@+id/metric_spinner_sms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:entries="@array/metric_array"
                    android:visibility="gone" />
            </RelativeLayout>

            <EditText
                android:id="@+id/amount_text_sms"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />

            <EditText
                android:id="@+id/ing_name_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:ems="10" />
        </TableRow>

        <!-- second ingredient -->



        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Directions" />

        <EditText
            android:id="@+id/directions_text_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine" />

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Options" />

        <Button
            android:id="@+id/save_button_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Save Recipe" />

            <Button
            android:id="@+id/dismiss_button_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Dismiss" />




        <!-- DONE -->

    </LinearLayout>

</ScrollView> 
    * File: SmsViewActivity.java

package com.example.chowpalv21;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

  public class SmsViewActivity extends Activity {



    private static final String TAG = "SmsViewActivity";

                private FoodJSONSerializer mSerializer;

                // protected TextView mReceivedFromView;

                public Button mSaveButton, mDismissButton;
                public int mNotificationId;
                public String message;
                private EditText mTitleField;
                private CheckBox mImperialCheckBox;
                private CheckBox mMetricCheckBox;
                private EditText mServingsField;
                private EditText mDirectionsField;

                // set up spinners. allows for up to ten ingredients.
                private Spinner mMetricSpinner;
                private Spinner mImperialSpinner;
                Food mFood;
                //----------------------

------------------------
            public void onCreate(Bundle savedInstanceState){
                setContentView(R.layout.view_sms);
                //super.onCreate(savedInstanceState);
            } 

            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                //View v = findViewById(R.layout.sms_view);
                    View v = super.onCreateView(name, context, attrs);

                 mSaveButton = (Button) findViewById(R.id.save_button_sms);
                 mDismissButton = (Button) findViewById(R.id.dismiss_button_sms);

                // ------------------------------------------------------------

                // Get extras and display information in view
                //String sender = getIntent().getStringExtra("sender");
                String msg = getIntent().getStringExtra("message");
                try {
                    JSONObject jsonRecipe = new JSONObject(msg);
                    Food mFood = new Food(jsonRecipe);
                    Log.i(TAG, "Food = " + mFood);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // -----------------------------------------------------------------------
                mNotificationId = getIntent().getIntExtra("notificationid", 0);
                if (mNotificationId == 0) {
                    Log.e(TAG, "Could not retrieve notification ID.");
                    Toast.makeText(this, "A fatal error has occurred in SMS viewer.",
                            Toast.LENGTH_LONG).show();
                    finish();
                }

                // Cancel the notification
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager notificationMgr = (NotificationManager) getSystemService(ns);
                notificationMgr.cancel(mNotificationId);

                // --------------------------------------------------
                mTitleField = (EditText)v. findViewById(R.id.food_title_sms);
                mTitleField.setText(mFood.getTitle());

                mServingsField = (EditText)v. findViewById(R.id.food_servings_sms);
                mServingsField.setText(mFood.getServings());

               mDirectionsField = (EditText)v. findViewById(R.id.directions_text_sms);
                mDirectionsField.setText(mFood.getDirections());


                // --------------------------------------------------

                // Listener for Save button click
                mSaveButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        finish();
                    }
                });

                // Listener for Dismiss button click
                mDismissButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        backToList();

                        finish();
                    }
                });
        return v;
            }

            public void backToList() {
                Intent i = new Intent(this, FoodListFragment.class);
                startActivity(i);
            }
        }
public class SmsViewActivity extends Activity {
    private static final String TAG = "SmsViewActivity";
    private FoodJSONSerializer mSerializer;
    public Button mSaveButton, mDismissButton;
    public int mNotificationId;
    public String message;
    private EditText mTitleField;
    private CheckBox mImperialCheckBox;
    private CheckBox mMetricCheckBox;
    private EditText mServingsField;
    private EditText mDirectionsField;
    private Spinner mMetricSpinner;
    private Spinner mImperialSpinner;
    Food mFood;

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.view_sms); mSaveButton = (Button) findViewById(R.id.save_button_sms); mDismissButton = (Button) findViewById(R.id.dismiss_button_sms); // ------------------------------------------------------------ // Get extras and display information in view //String sender = getIntent().getStringExtra("sender"); String msg = getIntent().getStringExtra("message"); try { JSONObject jsonRecipe = new JSONObject(msg); Food mFood = new Food(jsonRecipe); Log.i(TAG, "Food = " + mFood); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // ----------------------------------------------------------------------- mNotificationId = getIntent().getIntExtra("notificationid", 0); if (mNotificationId == 0) { Log.e(TAG, "Could not retrieve notification ID."); Toast.makeText(this, "A fatal error has occurred in SMS viewer.", Toast.LENGTH_LONG).show(); finish(); } // Cancel the notification String ns = Context.NOTIFICATION_SERVICE; NotificationManager notificationMgr = (NotificationManager) getSystemService(ns); notificationMgr.cancel(mNotificationId); // -------------------------------------------------- mTitleField = (EditText)v. findViewById(R.id.food_title_sms); mTitleField.setText(mFood.getTitle()); mServingsField = (EditText)v. findViewById(R.id.food_servings_sms); mServingsField.setText(mFood.getServings()); mDirectionsField = (EditText)v. findViewById(R.id.directions_text_sms); mDirectionsField.setText(mFood.getDirections()); // -------------------------------------------------- // Listener for Save button click mSaveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); } }); // Listener for Dismiss button click mDismissButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { backToList(); finish(); } }); return v; } public void backToList() { Intent i = new Intent(this, FoodListFragment.class); startActivity(i); } }