gpt4 book ai didi

java - 不幸的是<项目名称>已停止消息

转载 作者:行者123 更新时间:2023-12-01 13:19:42 25 4
gpt4 key购买 nike

我是 Android 新手,正在使用 Android studio。我创建了一个带有两个按钮的基本程序。但是一旦我启动模拟器,应用程序就不会启动。我收到一条消息,提示“不幸的是 -项目名称-已停止'。但是代码编译没有任何错误。这里我附上代码。

AndroidMAnifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectdrogo" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.projectdrogo.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>
</application>

</manifest>

这是我的 MainActivity.java 代码

package com.example.projectdrogo;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
int counter;
Button add,subtract;
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter =0;
add = (Button)findViewById(R.id.bAdd);
subtract=(Button)findViewById(R.id.bSubtract);
textview=(TextView)findViewById(R.id.textView);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter++;
textview.setText("Your Total is "+counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter--;
textview.setText("Your Total is "+counter);
}
});

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


@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;
}

@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);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

}

这是我的 main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.projectdrogo.MainActivity" >

<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.projectdrogo.MainActivity"
tools:ignore="MergeRootFrame" />

fragment_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.projectdrogo.MainActivity$PlaceholderFragment">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<TextView
android:text="@string/name"
android:textSize="50dp"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/textView" />


<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:id="@+id/bSubtract"
android:textSize="30dp"
android:layout_gravity="center"
/>

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Add One"
android:id="@+id/bAdd"
android:layout_gravity="center"
android:textSize="30dp" />

</LinearLayout>

</RelativeLayout>

请帮助我,我陷入困境。谢谢

最佳答案

activity_main.xml 没有按钮或 TextView 。看起来像是在 fragment 布局中

我猜你有一个 fragment

PlaceholderFragment newFragment = new  PlaceholderFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();

然后在Fragment的onCreateView中

 View rootView = inflater.inflate(R.layout.fragment_main, container, false);
add = (Button)rootView.findViewById(R.id.bAdd);
subtract=(Button)rootView.findViewById(R.id.bSubtract);
textview=(TextView)rootView.findViewById(R.id.name);
...// rest of the code

你有一个FrameLayout,它是一个容器。您需要将 fragment 添加到容器中。 Activity 中的 View 也属于 fragment 布局。因此,您需要按照建议在 onCreateView 中初始化相同的内容。

编辑:

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlaceholderFragment newFragment = new PlaceholderFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}

然后

公共(public)静态类 PlaceholderFragment 扩展 Fragment {

    int counter;
Button add,subtract;
TextView textview;
public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
add = (Button)rootView.findViewById(R.id.bAdd);
subtract=(Button)rootView.findViewById(R.id.bSubtract);
textview=(TextView)rootView.findViewById(R.id.name);
... // rest of the code

return rootView;
}
}

关于java - 不幸的是<项目名称>已停止消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com