我正在创建我的第一个应用程序但是当我在我的设备中安装apk文件时,当我尝试打开它时它会崩溃。我无法找到问题,因此我会发布代码的开头。该应用程序以SplashActivity
开头import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}
}
然后家庭活动应该开始
public class Home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
getIntent();
String[] names= new String[]{"Albums","By Era", "Members", "Eddie", "Fan Arts", "Ed Force One"};
int []imageId = new int[]{R.drawable.albums, R.drawable.era, R.drawable.members, R.drawable.eddie, R.drawable.fanarts, R.drawable.edforceone };
CustomGrid adapter = new CustomGrid( Home.this, names, imageId);
final GridView grid_main = (GridView) findViewById(R.id.grid_main);
grid_main.setAdapter(adapter);
grid_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// selection
String maincat="";
switch (grid_main.getCheckedItemPosition()){
case R.id.albums:
maincat="Albums";
break;
case R.id.era:
maincat="By Era";
break;
case R.id.member:
maincat="Members";
break;
case R.id.eddie:
maincat="Eddie";
break;
case R.id.arts:
maincat="Fan arts";
break;
case R.id.ed:
maincat="Ed Force One";
break;
}
//start different activities
if ( maincat.equals("Eddie") || maincat.equals("Ed Force One") || maincat.equals("Fan arts")){
Intent intent= new Intent(Home.this, Pictures.class);
intent.putExtra("maincat", maincat);
startActivity(intent);}
else {
Intent intent= new Intent(Home.this, SubActivity.class);
intent.putExtra("maincat", maincat);
startActivity(intent);}
}
});
}
@Override
// overflow menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.about:
Intent intent= new Intent(Home.this, About.class);
startActivity(intent);
return true;
case R.id.contacts:
Intent intent2= new Intent(Home.this, Contacts.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
以下是有关活动的清单部分
<activity android:name=".Home">
</activity>
<activity
android:name=".SubActivity"
android:parentActivityName=".Home" />
<activity
android:name=".About"
android:parentActivityName=".Home" />
<activity
android:name=".Contacts"
android:parentActivityName=".Home" />
<activity
android:name=".Pictures"
android:parentActivityName=".Home">
</activity>
<activity
android:name=".ViewPic"
android:parentActivityName=".Home">
</activity>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
该应用程序有一个巨大的资产文件夹,但即使删除它,该应用程序也无法运行。我验证了minimun sdk版本(19)与我的Android设备版本(4.4)相同。必须说我不能使用Android Studio模拟器,因为我的硬件已经过时了:/
答案 0 :(得分:1)
我无法发表评论,所以我必须使用asnwers。
这是什么意思%zu
?为什么不能从Android Studio测试它?
除此之外,还缺少很多代码。 I can't even test on my device because it doesn't have the option to do so
中有很多Activity
,但您只发布了2,Manifest
的{{1}}看起来不完整。
答案 1 :(得分:0)
SplashActivity.class中的更改:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, Home.class);
startActivity(i);
finish();
}
}, 1000);
}
}
AndroidManifest中的更改:
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Home"/>
更改Home.class:
public class Home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
****Your code*****
}
}
答案 2 :(得分:0)
您直接在onCreate
开始了您的活动,尝试按下按钮并在其上应用onClick
方法来打开您的新活动。像
Intent intent = new Intent(this, Home.class);
button.setOnClickListener(new View.OnClickListener(){
@override
public void onClick(View v){
startActivity(intent);
}
});
在此代码之前实例化您的按钮。