我是Android编程的初学者,这是我的第一个应用程序。
当我运行我无法理解的代码时,我在Java Eclipse中有一个错误列表。 他们中有一些 : 01-28 14:52:10.798:W / dalvikvm(347):threadid = 7:线程退出未捕获异常(组= 0x4001d800) 01-28 14:52:10.798:E / AndroidRuntime(347):致命异常:Thread-9 01-28 14:52:10.798:E / AndroidRuntime(347):android.content.ActivityNotFoundException:找不到处理Intent的活动{act = com.zewailiansapp.ust.FORMS} 01-28 14:52:10.798:E / AndroidRuntime(347):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) 01-28 14:52:10.798:E / AndroidRuntime(347):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 01-28 14:52:10.798:E / AndroidRuntime(347):在android.app.Activity.startActivityForResult(Activity.java:2817) 01-28 14:52:10.798:E / AndroidRuntime(347):在android.app.Activity.startActivity(Activity.java:2923) 01-28 14:52:10.798:E / AndroidRuntime(347):at com.zewailiansapp.ust.Mainactivity $ 4 $ 1.run(Mainactivity.java:124)
我的应用中的每个模板都会重复这些错误。
这是我的清单XML代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zewailiansapp.ust"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.zewailiansapp.ust.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.zewailiansapp.ust.Mainactivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.zewailiansapp.ust.Announcements"
android:label="Announcements" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.ANNOUNCEMENTS" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
<activity
android:name="com.zewailiansapp.ust.Calendar"
android:label="Online Calendar" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.CALENDAR" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
<activity
android:name="com.zewailiansapp.ust.Schedules"
android:label="Schedules" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.SCHEDULES" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
<activity
android:name="com.zewailiansapp.ust.Forms"
android:label="Forms" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.FORMS" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
<activity
android:name="com.zewailiansapp.ust.Social"
android:label="Social Links" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.SOCIAL" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
<activity
android:name="com.zewailiansapp.ust.Aboutus"
android:label="Credits" >
<intent-filter>
<action android:name="com.zewailiansapp.ust.ABOUTUS" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
</application>
</manifest>
这个主要活动Java代码
package com.zewailiansapp.ust;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class Mainactivity extends Activity {
ImageButton b1;
ImageButton b2;
ImageButton b3;
ImageButton b4;
ImageButton b5;
ImageButton b6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainactivity);
b1= (ImageButton) findViewById(R.id.ib1);
b2= (ImageButton) findViewById(R.id.ib2);
b3= (ImageButton) findViewById(R.id.ib3);
b4= (ImageButton) findViewById(R.id.ib4);
b5= (ImageButton) findViewById(R.id.ib5);
b6= (ImageButton) findViewById(R.id.ib6);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent changeWindow = new Intent("com.zewailiansapp.ust.ANNOUNCEMENTS");
startActivity(changeWindow);
}
}
};
timer.start();
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent changeWindow = new Intent("com.zewailiansapp.ust.CALENDAR");
startActivity(changeWindow);
}
}
};
timer.start();
}
});
b3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent changeWindow = new Intent("com.zewailiansapp.ust.SCHEDULES");
startActivity(changeWindow);
}
}
};
timer.start();
}
});
b4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent changeWindow = new Intent("com.zewailiansapp.ust.FORMS");
startActivity(changeWindow);
}
}
};
timer.start();
}
});
b5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent changeWindow = new Intent("com.zewailiansapp.ust.SOCIAL");
startActivity(changeWindow);
}
}
};
timer.start();
}
});
b6.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent changeWindow = new Intent("com.zewailiansapp.ust.ABOUTUS");
startActivity(changeWindow);
}
}
};
timer.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainactivity, menu);
return true;
}
}
这个主要的XML布局
<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/program_background"
>
<TextView
android:id="@+id/social"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="400dp"
android:text="Social Links" />
<TextView
android:id="@+id/forms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="300dp"
android:text="Forms" />
<TextView
android:id="@+id/aboutus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="400dp"
android:text="Credits" />
<TextView
android:id="@+id/announcements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="200dp"
android:text="Announcements" />
<TextView
android:id="@+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="200dp"
android:text="Calendar" />
<TextView
android:id="@+id/schedules"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="300dp"
android:text="Schedules" />
<ImageButton
android:id="@+id/ib6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="350dp"
android:src="@drawable/about" />
<ImageButton
android:id="@+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="150dp"
android:src="@drawable/announcement" />
<ImageButton
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="150dp"
android:src="@drawable/calendar" />
<ImageButton
android:id="@+id/ib5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="350dp"
android:src="@drawable/social" />
<ImageButton
android:id="@+id/ib4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="250dp"
android:src="@drawable/form" />
<ImageButton
android:id="@+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="250dp"
android:src="@drawable/schedule" />
</RelativeLayout>
答案 0 :(得分:0)
ClassCast异常意味着您正在尝试将ImageButton
转换为非{1}}。找到代码中看起来像((ImageButton) findViewById(R.id.imageButton))
的部分,并确保它引用的ID 实际上 ImageButton
,而不是其他内容,例如标准Button
或ImageView
。如果错误仍然存在,请发布您的xml,以便我可以提供更多帮助。
此外,在开始活动时,请使用方法new Intent(getApplicationContext(), <Activity class>
...(即new Intent(getApplicationContext(), MyActivity.class)
...它会比您尝试使用的方法更好。祝您好运!< / p>
答案 1 :(得分:0)
根据最新更新的LogCat,您现在遇到的问题与原始问题不同。以下是您的新问题的解决方案:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.zewailiansapp.ust.FORMS
据此,您的活动现已开始,您点击了其中一个按钮,但该应用无法找到您要告知其启动的课程。
使用以下代码段开始新活动:
Intent i = new Intent(MyFirstActivity.this, MySecondActivity.class);
startActivity(i);