我的應用程序名稱是timepass 下面的文件是MainActivity.java不幸的是[應用程序名稱]已停止
package com.example.timepass;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
int counter;
Button add, sub;
TextView Display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add= (Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
Display=(TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
Display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
Display.setText("Your total is " + counter);
}
});
}
@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;
}
}
下面的文件是activity_main.xml中被realted上述文件MainActivity.java
<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/pic_two">"
<Button
android:id="@+id/badd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvdisplay"
android:layout_alignTop="@+id/bsub"
android:text="@string/but1"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvdisplay"
android:layout_alignRight="@+id/tvdisplay"
android:text="@string/hello_world"
android:textSize="@dimen/asa" />
<TextView
android:id="@+id/tvdisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="@string/str1"
android:textColor="@layout/activity_main"
android:textSize="30sp" />
<Button
android:id="@+id/bsub"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/tvdisplay"
android:layout_below="@+id/tvdisplay"
android:layout_marginRight="38dp"
android:layout_marginTop="23dp"
android:text="@string/but2"
android:textSize="20sp" />
</RelativeLayout>
然後,我已經創建另一個活動其低於其Splash.java
package com.example.timepass;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity
{
@Override
protected void onCreate(Bundle tpsavedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(tpsavedInstanceState);
setContentView(R.layout.splash);
Thread timer =new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch(InterruptedException e)
{ {
e.printStackTrace();
}
finally
{
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
}
相關Splash.java然後,我已經創建的XML文件,該文件是splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/pic_background">
</LinearLayout>
現在我想先運行Splash.java文件,然後MainActivity.java文件,所以我必須做Androidmanifest.xml文件 中的一些更改是這些更改,並且在運行我的程序後,我收到錯誤「可惜timepass已停止」,其中timepass是我的應用程序名稱。請解決這個問題
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.timepass.Splash"
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="com.example.timepass.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.timepass.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
發佈帶有錯誤的logcat字符串 –
看,我知道你是Android開發新手,這就是爲什麼我向你提出這個建議:你可以隨時在Eclipse中查看Logcat中崩潰的原因。請學習如何通過它,然後你將面臨更少的問題。截至目前,只需發佈錯誤日誌,我會幫助你。 – rahulritesh