我目前正在尝试更改Android项目中的活动,但每次执行此操作时,都会收到应用程序错误。我是Android的新手,所以我可能犯了很多错误。我看了很多可能的答案,但没有发现任何可以帮助我的答案。
MainActivity
package com.android.azultech.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainScreen extends Activity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
final FloatingActionButton fab;
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Snackbar.make(view, "Open Developer Twitter", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
onTwitter(fab);
}
});
// final Button btn;
// btn = (Button) findViewById(R.id.btn);
// btn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// // Snackbar.make(view, "Test", Snackbar.LENGTH_LONG)
// // .setAction("Action", null).show();
// Intent intent = new Intent(view.getContext(), HelpScreen.class);
//
// view.getContext().startActivity(intent);
// }
// });
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void some_function(View v) {
Intent intent = new Intent(v.getContext(), HelpScreen.class);
v.getContext().startActivity(intent);
}
private void onTwitter(FloatingActionButton fab) {
Intent intent = null;
try {
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=3146095821"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/itsazul_"));
}
this.startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_help) {
Intent intent = new Intent();
intent.setClass(this, HelpScreen.class);
this.startActivity(intent);
}
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_quit) {
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"MainScreen Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.android.azultech.myapplication/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"MainScreen Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.android.azultech.myapplication/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
HelpScreen
package com.android.azultech.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class HelpScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_help_screen);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Open Developer Twitter", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
这里是在content_main_screen.xml
中声明按钮的位置<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Help"
android:id="@+id/btn"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/textView2"
android:onClick="some_function"/>
这是清单文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.azultech.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainScreen"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelpScreen" />
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
activity_main_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.android.azultech.myapplication.MainScreen">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main_screen" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
android:hapticFeedbackEnabled="false"
android:clickable="true"
android:duplicateParentState="true"
android:background="#ffffff"
android:rotation="0"
android:visibility="visible"
android:tint="#24a5b3" />
</android.support.design.widget.CoordinatorLayout>
content_main_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout 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"
android:layout_width="384dp"
android:layout_height="700dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.android.azultech.myapplication.MainScreen"
tools:showIn="@layout/activity_main_screen"
android:background="#4f4a4a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Shadow"
android:id="@+id/textView"
android:textStyle="bold"
android:textColor="#e4dddd"
android:textSize="40dp"
android:paddingTop="50dp"
android:paddingRight="25dp"
android:layout_centerHorizontal="true" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="262dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Please Note: This app will continue in development for the next few months. It's current state will not be the final one."
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:paddingRight="30dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Help"
android:id="@+id/btn"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/textView2"
android:onClick="some_function"/>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:0)
我认为这是因为你正在寻找fab按钮,但你想要btn按钮。但我不知道你在哪里夸大content_main_screen。
所以我要继续前进。首先它适用于您的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
/>
</RelativeLayout>
我要编辑尝试按照他的功能放置你的布局。
Edit2:它实际上可以在我的手机上运行。你能直接加入第一个活动或你的活动崩溃吗?你能告诉我你的logcat错误吗?(所有红线)你的手机上安装了Twitter吗?是崩溃的推特按钮还是帮助按钮?