startActivity的致命异常

时间:2012-12-25 12:16:40

标签: android

我有两个Activiti(Smena和Sdacha) 我按下Smena上的按钮并打开Sdacha。 我需要从textView Sdacha发送String到Smena。 当我按下Sdacha上的按钮时,我得到了ERROR

public class Sdacha extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdacha);    

        Button but10=(Button) findViewById(R.id.button10);
        but10.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        Intent intent1=new Intent(Sdacha.this,Smena.class);

                             startActivity(intent1);
                    }
            });

    }
}

>

 12-25 18:05:40.130: WARN/dalvikvm(378): threadid=1: thread exiting
> with uncaught exception (group=0x40014760) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378): FATAL EXCEPTION: main 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378): java.lang.RuntimeException: Unable to start
> activity ComponentInfo{com.example.master/com.example.master.Smena}:
> java.lang.NullPointerException 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.access$500(ActivityThread.java:122) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.os.Handler.dispatchMessage(Handler.java:99) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.os.Looper.loop(Looper.java:132) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.main(ActivityThread.java:4123) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> java.lang.reflect.Method.invokeNative(Native Method) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> java.lang.reflect.Method.invoke(Method.java:491) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378):     at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> dalvik.system.NativeStart.main(Native Method) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378): Caused by: java.lang.NullPointerException
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> com.example.hotdog_master.Smena.onCreate(Smena.java:42) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.Activity.performCreate(Activity.java:4397) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     ... 11 more

Smena Activiti这是收银员的活动 Smena活动代码:

public class Smena extends Activity 
{
    public Nomenklatura Asortiment=new Nomenklatura(100);
    public static final String DEBUG_TAG = "myLogs";
    ArrayList<String> data =new ArrayList<String> ();
    ArrayList<String> data2 =new ArrayList<String> (4);
    GridView gvMain;
    GridView gvMain2;
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;
    double Sum=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smena); 
        Asortiment=(Nomenklatura) getIntent().getExtras().getSerializable("Nomenklatura");

        data2.add("СУММА");
        data2.add("0");
        data2.add("СДАЧА");
        data2.add("0");

        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
        gvMain = (GridView) findViewById(R.id.gridView1);
        gvMain.setAdapter(adapter);

        adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, data2);
        gvMain2 = (GridView) findViewById(R.id.gridView2);
        gvMain2.setAdapter(adapter2);

        Button but1=(Button) findViewById(R.id.button1);
        but1.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        ButtonOn (1);
                    }
            });
        Button but21=(Button) findViewById(R.id.button21);
        but21.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        ButtonDel();
                    }
            });

        Button but22=(Button) findViewById(R.id.button22);
        but22.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        data.clear();
                        adapter.clear();

                    }
            });

        Button but23=(Button) findViewById(R.id.button23);
        but23.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        Intent intent1=new Intent(Smena.this,Login.class);
                        // intent1.putExtra("Sdacha", Asortiment);
                         startActivity(intent1);

                    }
            });
    }
    private void ButtonDel()
    {
        int cnt=    data.size();
        data.remove(cnt-1);
        data.remove(cnt-2);
        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
        gvMain.setAdapter(adapter);
    }

    private void ButtonOn (int Art)
    {
        String name=    Asortiment.name.get(1);
        String price=   Asortiment.price.get(1).toString();
        data.add(name);
        data.add(price);
        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
        gvMain.setAdapter(adapter); 
    }
    private void CheckSum()
    {
        Sum=0;
        for(int k=1;k<data.size();k=k+2)
        {
            Sum=Sum+Double.parseDouble(data.get(k));
        }
    }

}

2 个答案:

答案 0 :(得分:0)

请尝试这样

  startActivity(new Intent(this,Smena.class));

答案 1 :(得分:0)

如果定义和声明正确,请检查这些内容:

1)如果根据相应的XML,所有的id都是正确的。

2)活动在清单文件中定义。

3)检查Smena活动中的行号42是否有错误。

试试让我知道。如果可能,请包含这些文件的代码以供进一步探索。