Android意图 - 变得疯狂

时间:2015-06-19 19:25:39

标签: android android-intent

好吧,我整天都在编程,几乎到处搜索,但还没有。我希望,你帮助我一点点。 我有两个活动,我希望他们进行沟通。这是代码

MainActivity

public class MainActivity extends Activity{

    ImageView imgsettings;

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

        imgsettings = (ImageView) findViewById(R.id.imgviewsettings);        
        imgsettings.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AppSettings.class);
                startActivity(intent);
            }
        });
    }
}

清单

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AppSettings"
            android:label="@string/title_activity_app_settings" > 
        </activity>
    </application>

的AppSettings

package com.example.oldie;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AppSettings extends Activity implements OnClickListener {

    TextView lblkeyword, txtkeyword;
    TextView lblnumber, txtnumber;
    TextView lbladdress, txtaddress;
    Button btnok;
    String filename = "preferences.txt";
    public static int count = 0;
    private static String keyword = "";


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

        lblkeyword = (TextView) findViewById(R.id.lblkeyword);
        txtkeyword = (TextView) findViewById(R.id.txtkeyword);
        lblnumber = (TextView) findViewById(R.id.lblnumber);
        txtnumber = (TextView) findViewById(R.id.txtnumber);
        lbladdress = (TextView) findViewById(R.id.lbladdress);
        txtaddress = (TextView) findViewById(R.id.txtaddress);
        btnok = (Button) findViewById(R.id.btnok);

        btnok.setOnClickListener(this);
        lblkeyword.setOnClickListener(this);
        lblnumber.setOnClickListener(this);
        lbladdress.setOnClickListener(this);

        //metritis gia na emfanisei mono mia fora to minima
        if(count==0){
            Toast.makeText(getApplicationContext(), "Tab on the labels for more information", Toast.LENGTH_LONG).show();
            count = count + 1;
        }


        updateUIFromFiles();

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.lblkeyword:
            Toast.makeText(getApplicationContext(), "Set a keyword set that will trigger the SMS checker", Toast.LENGTH_SHORT).show();
            break;
        case R.id.lblnumber:
            Toast.makeText(getApplicationContext(), "Set a number that the app will communicate in case of emmergency", Toast.LENGTH_SHORT).show();
            break;
        case R.id.lbladdress:
            Toast.makeText(getApplicationContext(), "Set the home address\nex. Agias Lavras 19, Piraeus, Greece", Toast.LENGTH_SHORT).show();
            break;
        case R.id.btnok:
            if((txtkeyword.getText().toString().equals("")) || (txtaddress.getText().toString().equals("")) ||(txtnumber.getText().toString().equals(""))){
                Toast.makeText(this, "Please fill all the Text Fields.", Toast.LENGTH_SHORT).show();
            }
            else{
                savePreferences();
                keyword = txtkeyword.getText().toString();
                finish();
            }
            break;
        }

    }

    private void savePreferences() {
        //thewrithike anagkaio na apothikeutoun ta stoixeia se ena arxeio gia na mi xathoun.
        try{
            OutputStreamWriter outfile = new OutputStreamWriter(openFileOutput(filename, 0));
            outfile.write(txtkeyword.getText().toString() + "\n" + txtnumber.getText().toString() + "\n" + txtaddress.getText().toString());
            outfile.close();
            Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();
        }
        catch(Throwable t) {
            Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
            }
    }

    private void updateUIFromFiles() {
        try { 
            InputStream infile = openFileInput(filename);
            if (infile != null){
                InputStreamReader tmp = new InputStreamReader(infile);
                BufferedReader reader = new BufferedReader(tmp);
                txtkeyword.setText(reader.readLine());
                txtnumber.setText(reader.readLine());
                txtaddress.setText(reader.readLine());
                infile.close();
            }
        }
        catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
        }
        catch (Throwable t) {
            Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
        }
    }

    public String GetMessage(){ //Methodos gia na parei to service to keyword.
        return keyword;
    }

}

问题是

06-19 22:17:03.789: E/AndroidRuntime(17204): FATAL EXCEPTION: main
06-19 22:17:03.789: E/AndroidRuntime(17204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oldie/com.example.oldie.AppSettings}: java.lang.NullPointerException
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.os.Looper.loop(Looper.java:137)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at dalvik.system.NativeStart.main(Native Method)
06-19 22:17:03.789: E/AndroidRuntime(17204): Caused by: java.lang.NullPointerException
06-19 22:17:03.789: E/AndroidRuntime(17204):    at com.example.oldie.AppSettings.onCreate(AppSettings.java:39)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-19 22:17:03.789: E/AndroidRuntime(17204):    ... 11 more

Intent的代码根本不起作用。我的应用程序崩溃并结束。我在吃药以控制我的愤怒之前迈出了一步......我&#39;不知道什么是错的,意图不起作用。请帮帮我。

3 个答案:

答案 0 :(得分:1)

在AppSettings活动中,您将内容视图设置为R.layout.activity_main。我猜不是你想要的。它可能没有您期望找到的TextView。查找它们的调用返回null,这会在您调用setOnClickListener()时导致异常。

答案 1 :(得分:0)

如果没有logcat,我无法确定问题究竟是什么,但是您可以尝试以下方法:

1。)尝试在Intent构造函数中使用getApplicationContext()而不是MyActivity.this。

2.)检查AppSettings类是否与MainActivity类位于同一个包中。

如果您发布相关的logcat,我会尝试提供更多帮助

答案 2 :(得分:0)

您在AppSettings活动中使用了错误的布局:

setContentView(R.layout.activity_main);

这与你的MainActivity相同,这可能不对。结果你在这里找不到按钮:

btnok = (Button) findViewById(R.id.btnok);

然后将为null,当您尝试在此处设置onClickListener时:

btnok.setOnClickListener(this);

你得到NullPointerException