Android开发初学者 - 错误

时间:2013-04-21 04:38:26

标签: android

好吧,我正在做这个应用程序只是为了试用。这就是错误。它说:

04-21 04:11:41.618: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-21 04:11:41.618: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.windteste/com.example.windteste.MainActivity}: java.lang.NullPointerException
04-21 04:11:41.618: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-21 04:11:41.618: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 04:11:41.618: E/AndroidRuntime(333):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)

..还有更多错误。因此,这两种方法(DfigMainActivity):

package com.example.windteste;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Dfig extends Activity{

    @Override
    protected void onCreate(Bundle blablu) {
        // TODO Auto-generated method stub
        super.onCreate(blablu);
        setContentView(R.layout.activity_main);
        MediaPlayer windsound = MediaPlayer.create(Dfig.this, R.raw.windsound);
        windsound.start();
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(4000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent openMain = new Intent("com.example.windteste.MAIN");
                    startActivity(openMain);
                }
            }
        };
        timer.start();
        windsound.release();
    }





}





 package com.example.windteste;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.text.InputType;

----------------------------------------------------------------------------------


public class MainActivity extends Activity {

    Button check = (Button) findViewById(R.id.calculator);
    final EditText UserInput = (EditText) findViewById(R.id.windspeed);
    final TextView P = (TextView) findViewById(R.id.potencia);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);



    UserInput.setInputType(InputType.TYPE_CLASS_NUMBER);


    check.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


        Number Ws = (Number) UserInput.getText();
        P.setText("Your total is " + Ws);

        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    } 



}

好吧,我希望你能帮帮我 谢谢!

2 个答案:

答案 0 :(得分:2)

尝试:

public class MainActivity extends Activity {

    Button check;
    final EditText UserInput;
    final TextView P;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);

        check = (Button) findViewById(R.id.calculator);
        UserInput = (EditText) findViewById(R.id.windspeed);
        P = (TextView) findViewById(R.id.potencia);

    //Rest is same 

在调用setContentView()之后才能使用findViewById()。

答案 1 :(得分:0)

确保清单中列出了这两项活动。它的外观是您的清单文件中未列出MainActivity。