-2

I found myself in trouble with creating new activity, I get the unfortunately your app has stopped error message and as a good humanbeing I thought I'd share my misfortune with you:

I can't get my button to open new activity:

MainActivity.java:

package com.example.vogella.dev;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {



private EditText text;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (EditText) findViewById(R.id.editText1);

}


  public void onClick(View view) { 
  switch (view.getId()) { 
  case R.id.button1:

RadioButton celsiusButton =(RadioButton)findViewById(R.id.radio0);
RadioButtonfahrenheitButton=(RadioButton)findViewById(R.id.radio1);

 if (text.getText().length() == 0) {

 Toast.makeText(this,getResources().getString(R.string.toast_a),
 Toast.LENGTH_LONG).show(); 
       return;
        }

float inputValue =  Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {

text.setText(Strgin.valuof(ConvertFahrenheitToCelsius(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);

} else {

text.setText(Strgin.valuof(ConvertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);             



        }
    break;

    }
    }

private float convertFahrenheitToCelsius(float fahrenheit) {
    return ((fahrenheit - 32) * 5 / 9);

    }

private float ConvertCelsiusToFahrenheit(float celsius)   {
    return ((celsius * 9) / 5) +32;
    } 

    public void scrollview(View v) {
    Intent intent =newIntent(this,ScrollviewActivity.class);
    startActivity(intent);

    }       

    }

My activity_main.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/
android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myColor" >


  <EditText
  android:id="@+id/editText1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:ems="10"
  android:hint="@string/hint"
  android:inputType="numberDecimal|numberSigned" >

    <requestFocus />
 </EditText>

  <RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@+id/editText1">

  <RadioButton
      android:id="@+id/radio0"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="@string/celsius" />

  <RadioButton 
      android:id="@+id/radio1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/fahrenheit" />

      </RadioGroup>

  <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/radioGroup1"
      android:onClick="onClick"
      android:text="@string/calc" />

  <Button
       android:id="@+id/test_button"
       style="?android:attr/buttonStyleSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:layout_marginTop="44dp"
       android:layout_toRightOf="@+id/button1"
       android:text="@string/test_button"
       android:onClick="scrollview"/>               

        </RelativeLayout>    

The AndroidManifest in portion

   <activity
   android:name="com.example.vogella.dev.Scrollview"
   android:label="@string/title_activity_scrollview"

   android:parentActivityName=com.example.vogella.dev.MainActivity">        

    <meta-data 

    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.example.vogella.dev.MainActivity" />

    </activity>
   </application>

And the activity I'm trying to open

public class ScrollviewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview);
TextView view = (TextView) findViewById(R.id.TextView02);
String s="";
for (int i=0; i < 500; i++) {
s += "vogella.com" ;
}   
view.setText(s);
}
}
4

7 回答 7

0
Intent intent =newIntent(this,ScrollviewActivity.class);

你忘记了 new 和 Intent 之间的空间。但是,您也可能遇到其他问题。告诉我们抛出的错误会有所帮助。

于 2013-09-08T09:26:45.177 回答
0

您在 logcat 中遇到的错误是什么。可能是您没有在清单文件中提到您的 ScrollViewActivity。请检查

于 2013-09-08T09:28:29.150 回答
0

您的清单必须包含MainActivity

<activity android:name="com.example.vogella.dev.MainActivity" />
于 2013-09-08T09:28:31.707 回答
0

即使你编译IDE没有实现错误

  Intent intent =newIntent(this,ScrollviewActivity.class); // Error is here
    startActivity(intent);

应该是这样的

  Intent intent =new Intent(this,ScrollviewActivity.class);
    startActivity(intent);

并且永远不要忘记将您的每一项新活动添加到清单中

    <activity
        android:name="com.activities.ActivitySettings"
        android:icon="@drawable/icon_small"
        android:label="@string/title_activity_activity_settings"
        android:logo="@drawable/icon_small"
        android:theme="@style/MyTheme" >
    </activity>

专门针对您的问题

你有两个活动MainActivityScrollViewActivity

在清单上你提到这个

  <activity
   android:name="com.example.vogella.dev.Scrollview"
   android:label="@string/title_activity_scrollview"

Scrollview不是您的任何活动,ScrollViewActivity而是

于 2013-09-08T10:06:47.957 回答
0

1)在 onCreate() 中使用它

按钮按钮 = (Button) findViewById(R.id.button1);

您确实初始化了EditText那么为什么不为名为“ button1 ”的Button执行此操作

2) 甚至在android manifest中提及你的ScrollViewActivity

3) Intent i = new Intent (getApplicationContext,ScrollViewActivity.class); 开始活动(一);

于 2013-09-08T09:35:40.597 回答
0

您是否在 Manifest 中声明了新的 Activity?如果没有,请像这样添加:

<activity android:name="com.example.vogella.dev.ScrollviewActivity" />
于 2013-09-08T09:43:08.253 回答
0
  Public void launchActivity(View view){
  Intent intent = new Intent(this, youractivityname
  Class);
  startActivity(intent);
于 2015-04-01T07:29:55.617 回答