0

我正在尝试使用以下代码从一个活动屏幕移动到另一个活动屏幕。第一个活动 MainActivity 只有一个按钮单击它我打算调用另一个活动 Menactivity 但是当我单击该按钮时没有任何反应,有时我会收到提示“不幸的是,应用程序已停止工作。我已经添加了第二个活动我的清单也是。我收到 Nullpointer 异常作为错误并且线程无法启动。请帮助。

      package com.example.samplexml;




        import android.os.Bundle;
        import android.app.Activity;
        import android.content.Intent;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;

     //First Activity (Mainactivity)

        public class MainActivity extends Activity {
           @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
               Button b2= (Button) findViewById(R.id.button2);

               b2.setOnClickListener(new OnClickListener() {
                      @Override
                      public void onClick(View v) {

                          Intent intent = new Intent(MainActivity.this, MenActivity.class);
                            startActivity(intent);
                      }}); 

           }
        }


//Second Activity(Menactivity)

package com.example.samplexml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.Document;
import org.w3c.dom.Element;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MenActivity extends Activity {

    TextView  myTextView;
     EditText E1;
     EditText E2;
     EditText E3;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);


  Button b1= (Button) findViewById(R.id.button1);
  Button b2= (Button) findViewById(R.id.button3);
  Button b3= (Button) findViewById(R.id.button3);
   E1 = (EditText) findViewById(R.id.editText1);
   E2 = (EditText) findViewById(R.id.editText2);
   E3 = (EditText) findViewById(R.id.editText3);

  b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) { 




            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            try {
                docBuilder = docFactory.newDocumentBuilder();


            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("Class");
            doc.appendChild(rootElement);

            // staff elements
            Element student = doc.createElement("Student");
            rootElement.appendChild(student);


            // firstname elements
            Element firstname = doc.createElement("firstname");
            firstname.appendChild(doc.createTextNode(E1.getText().toString()));
            student.appendChild(firstname);

            Element Email = doc.createElement("Email");
            Email.appendChild(doc.createTextNode(E2.getText().toString()));
            student.appendChild(Email);

            // nickname elements
            Element Roll = doc.createElement("Roll_No");
            Roll.appendChild(doc.createTextNode(E3.getText().toString()));
            student.appendChild(Roll);


                    // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();



                Transformer transformer = transformerFactory.newTransformer();


            DOMSource source = new DOMSource(doc);

            File FF=new File(Environment.getExternalStorageDirectory()+"//new1.xml");
            //File FF=new File(Environment.getExternalStorageDirectory()+"//new.xml");
            try {
                if (FF.exists());
                    FF.delete();
                FF.createNewFile();
            } catch (IOException e) {
                // Handle the error here! don't ignore it. Either throw the exception all the way, or log it, or something.
                e.printStackTrace();
            }

            StreamResult result = new StreamResult(FF);
        transformer.transform(source, result);

                }
             catch (ParserConfigurationException e) {

                e.printStackTrace();
            }
             catch (TransformerException e) {

                    e.printStackTrace();}  
            Toast.makeText(getApplicationContext(), 
                    Environment.getExternalStorageDirectory().toString(), Toast.LENGTH_LONG).show();


                                }
    });
  b2.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {

          Intent intent = new Intent(MenActivity.this, MainActivity.class);
            startActivity(intent);
      }}); 

  b3.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
          // Close the application
          finish(); }});
  }
}
4

1 回答 1

0

根据您提供的唯一一条调试信息——你得到一个 NullPointerException——看起来你的意图被正确地触发了,问题在于MenActivity.

很可能,该活动的膨胀布局不包含button1,button2和/或的 ID button3

一些非常基本的调试会指出您的问题所在的确切位置。

于 2013-05-11T15:04:15.290 回答