android StartActivity崩溃

时间:2015-05-24 11:31:32

标签: java android eclipse android-intent start-activity

我正在尝试进行下一个活动,即Nieuw_huis1 这是我的主要活动代码,其中SetupNieuwHuis应该重定向到下一页

ln -s

但出于某种原因,当我启动我的应用程序时,我点击按钮btnNieuwHuis,它崩溃并给我这个错误日志

    package com.jan.energyservice;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.PrintStream;

    import android.support.v7.app.ActionBarActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MainActivity extends ActionBarActivity {
private EditText test;
private String data;
private String file = "mydata";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    test = (EditText) (findViewById(R.id.txtTest));

    setupNieuwHuis();
    slaietsop();
    tooniets();
}

    private void tooniets() {
        // 1 Reference
            Button Movebutton = (Button) findViewById(R.id.btnLees);
            // 2 click listener to run code
            Movebutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    read(test);
}});}

    private void slaietsop() {
    // 1 Reference
        Button Movebutton = (Button) findViewById(R.id.btnTest);
        // 2 click listener to run code
        Movebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save(test);
                                        }
            });
        }
    public void save(View view){
          data = test.getText().toString();
          try {
             FileOutputStream fOut = openFileOutput(file,Context.MODE_PRIVATE);
             new PrintStream(fOut).println("Naam van eerste ding");
             fOut.write(data.getBytes());
             fOut.close();
             Toast.makeText(getBaseContext(),"file saved",
             Toast.LENGTH_SHORT).show();
          } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
    public void read(View view){
          try{
             FileInputStream fin = openFileInput(file);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                temp = temp + Character.toString((char)c);
             }
             test.setText(temp);
             Toast.makeText(getBaseContext(),"file read",
             Toast.LENGTH_SHORT).show();

          }catch(Exception e){
              }
          }

private void setupNieuwHuis() {
    // 1 Reference
    Button Movebutton = (Button) findViewById(R.id.btnNieuwHuis);


    // 2 click listener to run code
    Movebutton.setOnClickListener(new View.OnClickListener() {

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

    }

此外,我的活动在AndroidManifest

中声明
05-24 10:51:16.466: E/Trace(825): error opening trace file: No such file or directory (2)
05-24 10:51:18.707: D/gralloc_goldfish(825): Emulator without GPU emulation detected.
05-24 10:51:29.166: D/AndroidRuntime(825): Shutting down VM
05-24 10:51:29.166: W/dalvikvm(825): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-24 10:51:29.196: E/AndroidRuntime(825): FATAL EXCEPTION: main
05-24 10:51:29.196: E/AndroidRuntime(825): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jan.energyservice/com.jan.energyservice.Nieuw_huis1}; have you declared this activity in your AndroidManifest.xml?
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.Activity.startActivityForResult(Activity.java:3370)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.Activity.startActivityForResult(Activity.java:3331)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.Activity.startActivity(Activity.java:3566)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.Activity.startActivity(Activity.java:3534)
05-24 10:51:29.196: E/AndroidRuntime(825):  at com.jan.energyservice.MainActivity$3.onClick(MainActivity.java:98)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.view.View.performClick(View.java:4204)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.view.View$PerformClick.run(View.java:17355)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.os.Handler.handleCallback(Handler.java:725)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.os.Looper.loop(Looper.java:137)
05-24 10:51:29.196: E/AndroidRuntime(825):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-24 10:51:29.196: E/AndroidRuntime(825):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:51:29.196: E/AndroidRuntime(825):  at java.lang.reflect.Method.invoke(Method.java:511)
05-24 10:51:29.196: E/AndroidRuntime(825):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-24 10:51:29.196: E/AndroidRuntime(825):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-24 10:51:29.196: E/AndroidRuntime(825):  at dalvik.system.NativeStart.main(Native Method)

这是修复Manifest后我得到的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jan.energyservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
<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=".jan.energyservice.Nieuw_huis1"
        android:label="@string/title_activity_nieuw_huis1" >
    </activity>
    <activity
        android:name=".Nieuw_huis2"
        android:label="@string/title_activity_nieuw_huis2" >
    </activity>
    <activity
        android:name=".Nieuw_huis3"
        android:label="@string/title_activity_nieuw_huis3" >
    </activity>
    <activity
        android:name=".Nieuw_huis4"
        android:label="@string/title_activity_nieuw_huis4" >
    </activity>
</application>

4 个答案:

答案 0 :(得分:1)

此部分不正确,此处的包名称不正确,请尝试此操作,

 <activity
    android:name="com.jan.energyservice.Nieuw_huis1"
    android:label="@string/title_activity_nieuw_huis1" >
</activity>

答案 1 :(得分:0)

  

您是否在AndroidManifest.xml中声明了此活动?

将您的Activity添加到AndroidManifest.xml

答案 2 :(得分:0)

<activity
   android:name=".Nieuw_huis1"
   android:label="@string/title_activity_nieuw_huis1" >
</activity>

用此更新你的Menifest文件。

答案 3 :(得分:0)

正如一些人所说,我的第一个错误是我的清单中的一个问题。 第二种是我的第二种形式(nieuw_huis1)

中的按钮误报

感谢您从我们那里得到的所有帮助!